From 9726f0f00ef7a42f940f57e5c02e4572a7bd998d Mon Sep 17 00:00:00 2001 From: Georgii Borovinskikh Date: Wed, 24 Jan 2024 16:47:43 +0100 Subject: [PATCH 1/4] Add CredentialsListener --- .../Credentials/CredentialsListenerTests.cs | 58 +++++++++++++++ src/SLCore/Common/Models/TokenDto.cs | 44 +++++++++++ .../Credentials/CredentialsListener.cs | 74 +++++++++++++++++++ src/SLCore/Listener/CredentialsListener.cs | 39 ++++++++++ 4 files changed, 215 insertions(+) create mode 100644 src/SLCore.UnitTests/Listener/Credentials/CredentialsListenerTests.cs create mode 100644 src/SLCore/Common/Models/TokenDto.cs create mode 100644 src/SLCore/Listener/Credentials/CredentialsListener.cs create mode 100644 src/SLCore/Listener/CredentialsListener.cs diff --git a/src/SLCore.UnitTests/Listener/Credentials/CredentialsListenerTests.cs b/src/SLCore.UnitTests/Listener/Credentials/CredentialsListenerTests.cs new file mode 100644 index 0000000000..47c4622344 --- /dev/null +++ b/src/SLCore.UnitTests/Listener/Credentials/CredentialsListenerTests.cs @@ -0,0 +1,58 @@ +/* + * SonarLint for Visual Studio + * Copyright (C) 2016-2024 SonarSource SA + * mailto:info AT sonarsource DOT com + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3 of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ + +using System; +using System.Threading.Tasks; +using SonarLint.VisualStudio.SLCore.Core; +using SonarLint.VisualStudio.SLCore.Listener.Credentials; +using SonarLint.VisualStudio.TestInfrastructure; + +namespace SonarLint.VisualStudio.SLCore.UnitTests.Listener.Credentials; + +[TestClass] +public class CredentialsListenerTests +{ + [TestMethod] + public void MefCtor_CheckIsExported() + { + MefTestHelpers.CheckTypeCanBeImported(); + } + + [TestMethod] + public void MefCtor_CheckIsSingleton() + { + MefTestHelpers.CheckIsSingletonMefComponent(); + } + + [TestMethod] + public async Task GetCredentialsAsync_StubImpl_ReturnsNoCredentials() + { + var testSubject = CreateTestSubject(); + + var response = await testSubject.GetCredentialsAsync(new GetCredentialsParams($"randomstring-{Guid.NewGuid()}")); + + response.Should().BeSameAs(GetCredentialsResponse.NoCredentials); + } + + private CredentialsListener CreateTestSubject() + { + return new CredentialsListener(); + } +} diff --git a/src/SLCore/Common/Models/TokenDto.cs b/src/SLCore/Common/Models/TokenDto.cs new file mode 100644 index 0000000000..ec130985b3 --- /dev/null +++ b/src/SLCore/Common/Models/TokenDto.cs @@ -0,0 +1,44 @@ +/* + * SonarLint for Visual Studio + * Copyright (C) 2016-2024 SonarSource SA + * mailto:info AT sonarsource DOT com + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3 of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ + +namespace SonarLint.VisualStudio.SLCore.Common.Models +{ + public class TokenDto + { + public TokenDto(string token) + { + this.token = token; + } + + public string token { get; } + } + + public class UsernamePasswordDto + { + public UsernamePasswordDto(string username, string password) + { + this.username = username; + this.password = password; + } + + public string username { get; } + public string password { get; } + } +} diff --git a/src/SLCore/Listener/Credentials/CredentialsListener.cs b/src/SLCore/Listener/Credentials/CredentialsListener.cs new file mode 100644 index 0000000000..b1ffe0c1e4 --- /dev/null +++ b/src/SLCore/Listener/Credentials/CredentialsListener.cs @@ -0,0 +1,74 @@ +/* + * SonarLint for Visual Studio + * Copyright (C) 2016-2024 SonarSource SA + * mailto:info AT sonarsource DOT com + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3 of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ + +using System.ComponentModel.Composition; +using System.Threading.Tasks; +using Newtonsoft.Json; +using SonarLint.VisualStudio.SLCore.Common.Models; +using SonarLint.VisualStudio.SLCore.Core; +using SonarLint.VisualStudio.SLCore.Protocol; + +namespace SonarLint.VisualStudio.SLCore.Listener.Credentials +{ + /// + /// Credentials provider for SLCore + /// + [Export(typeof(ISLCoreListener))] + [PartCreationPolicy(CreationPolicy.Shared)] + internal class CredentialsListener : ISLCoreListener + { + public Task GetCredentialsAsync(GetCredentialsParams parameters) + { + // stub implementation + return Task.FromResult(GetCredentialsResponse.NoCredentials); + } + } + + internal class GetCredentialsParams + { + public GetCredentialsParams(string connectionId) + { + this.connectionId = connectionId; + } + + public string connectionId { get; } + } + + internal class GetCredentialsResponse + { + // credentials property is nullable on the SLCore side + public static GetCredentialsResponse NoCredentials = new GetCredentialsResponse(); + + private GetCredentialsResponse(){} + + public GetCredentialsResponse(TokenDto token) + { + this.credentials = Either.CreateLeft(token); + } + + public GetCredentialsResponse(UsernamePasswordDto usernamePassword) + { + this.credentials = Either.CreateRight(usernamePassword); + } + + [JsonConverter(typeof(Either))] + public Either credentials { get; } + } +} diff --git a/src/SLCore/Listener/CredentialsListener.cs b/src/SLCore/Listener/CredentialsListener.cs new file mode 100644 index 0000000000..9af51f980f --- /dev/null +++ b/src/SLCore/Listener/CredentialsListener.cs @@ -0,0 +1,39 @@ +/* + * SonarLint for Visual Studio + * Copyright (C) 2016-2024 SonarSource SA + * mailto:info AT sonarsource DOT com + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3 of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ + +using SonarLint.VisualStudio.SLCore.Core; + +namespace SonarLint.VisualStudio.SLCore.Listener +{ + internal class CredentialsListener : ISLCoreListener + { + + } + + internal class GetCredentialsParams + { + public GetCredentialsParams(string connectionId) + { + this.connectionId = connectionId; + } + + public string connectionId { get; } + } +} From 142ba393150b7b9ce0a6d3adf08c74c6332a3b11 Mon Sep 17 00:00:00 2001 From: Georgii Borovinskikh Date: Wed, 24 Jan 2024 16:48:48 +0100 Subject: [PATCH 2/4] Move class to separate file --- src/SLCore/Common/Models/TokenDto.cs | 12 ------- .../Common/Models/UsernamePasswordDto.cs | 34 +++++++++++++++++++ 2 files changed, 34 insertions(+), 12 deletions(-) create mode 100644 src/SLCore/Common/Models/UsernamePasswordDto.cs diff --git a/src/SLCore/Common/Models/TokenDto.cs b/src/SLCore/Common/Models/TokenDto.cs index ec130985b3..193c28286b 100644 --- a/src/SLCore/Common/Models/TokenDto.cs +++ b/src/SLCore/Common/Models/TokenDto.cs @@ -29,16 +29,4 @@ public TokenDto(string token) public string token { get; } } - - public class UsernamePasswordDto - { - public UsernamePasswordDto(string username, string password) - { - this.username = username; - this.password = password; - } - - public string username { get; } - public string password { get; } - } } diff --git a/src/SLCore/Common/Models/UsernamePasswordDto.cs b/src/SLCore/Common/Models/UsernamePasswordDto.cs new file mode 100644 index 0000000000..c89ce248fe --- /dev/null +++ b/src/SLCore/Common/Models/UsernamePasswordDto.cs @@ -0,0 +1,34 @@ +/* + * SonarLint for Visual Studio + * Copyright (C) 2016-2024 SonarSource SA + * mailto:info AT sonarsource DOT com + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3 of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ + +namespace SonarLint.VisualStudio.SLCore.Common.Models +{ + public class UsernamePasswordDto + { + public UsernamePasswordDto(string username, string password) + { + this.username = username; + this.password = password; + } + + public string username { get; } + public string password { get; } + } +} From 185383a33461ae811de93f349e9d66a8161887c6 Mon Sep 17 00:00:00 2001 From: Georgii Borovinskikh Date: Wed, 24 Jan 2024 16:49:28 +0100 Subject: [PATCH 3/4] Delete file --- src/SLCore/Listener/CredentialsListener.cs | 39 ---------------------- 1 file changed, 39 deletions(-) delete mode 100644 src/SLCore/Listener/CredentialsListener.cs diff --git a/src/SLCore/Listener/CredentialsListener.cs b/src/SLCore/Listener/CredentialsListener.cs deleted file mode 100644 index 9af51f980f..0000000000 --- a/src/SLCore/Listener/CredentialsListener.cs +++ /dev/null @@ -1,39 +0,0 @@ -/* - * SonarLint for Visual Studio - * Copyright (C) 2016-2024 SonarSource SA - * mailto:info AT sonarsource DOT com - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 3 of the License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program; if not, write to the Free Software Foundation, - * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - */ - -using SonarLint.VisualStudio.SLCore.Core; - -namespace SonarLint.VisualStudio.SLCore.Listener -{ - internal class CredentialsListener : ISLCoreListener - { - - } - - internal class GetCredentialsParams - { - public GetCredentialsParams(string connectionId) - { - this.connectionId = connectionId; - } - - public string connectionId { get; } - } -} From b69d69898679b71235dde0a8220dffc6b3815af9 Mon Sep 17 00:00:00 2001 From: Georgii Borovinskikh Date: Thu, 25 Jan 2024 10:38:03 +0100 Subject: [PATCH 4/4] Add tests & fix bugs --- src/SLCore.UnitTests/Common/TokenDtoTests.cs | 46 +++++++++ .../Common/UserAndPasswordDtoTests.cs | 50 ++++++++++ .../Credentials/GetCredentialsParamsTests.cs | 38 ++++++++ .../GetCredentialsResponseTests.cs | 96 +++++++++++++++++++ src/SLCore/Common/Models/TokenDto.cs | 4 +- .../Common/Models/UsernamePasswordDto.cs | 6 +- .../Credentials/CredentialsListener.cs | 34 ------- .../Credentials/GetCredentialsParams.cs | 32 +++++++ .../Credentials/GetCredentialsResponse.cs | 48 ++++++++++ 9 files changed, 317 insertions(+), 37 deletions(-) create mode 100644 src/SLCore.UnitTests/Common/TokenDtoTests.cs create mode 100644 src/SLCore.UnitTests/Common/UserAndPasswordDtoTests.cs create mode 100644 src/SLCore.UnitTests/Listener/Credentials/GetCredentialsParamsTests.cs create mode 100644 src/SLCore.UnitTests/Listener/Credentials/GetCredentialsResponseTests.cs create mode 100644 src/SLCore/Listener/Credentials/GetCredentialsParams.cs create mode 100644 src/SLCore/Listener/Credentials/GetCredentialsResponse.cs diff --git a/src/SLCore.UnitTests/Common/TokenDtoTests.cs b/src/SLCore.UnitTests/Common/TokenDtoTests.cs new file mode 100644 index 0000000000..ca97a3e483 --- /dev/null +++ b/src/SLCore.UnitTests/Common/TokenDtoTests.cs @@ -0,0 +1,46 @@ +/* + * SonarLint for Visual Studio + * Copyright (C) 2016-2024 SonarSource SA + * mailto:info AT sonarsource DOT com + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3 of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ + +using System; +using SonarLint.VisualStudio.SLCore.Common.Models; + +namespace SonarLint.VisualStudio.SLCore.UnitTests.Common; + +[TestClass] +public class TokenDtoTests +{ + [TestMethod] + public void Ctor_SetsPropertiesCorrectly() + { + var token = "token123"; + + var testSubject = new TokenDto(token); + + testSubject.token.Should().BeSameAs(token); + } + + [TestMethod] + public void Ctor_NullParameter_Throws() + { + Action act = () => new TokenDto(null); + + act.Should().ThrowExactly(); + } +} diff --git a/src/SLCore.UnitTests/Common/UserAndPasswordDtoTests.cs b/src/SLCore.UnitTests/Common/UserAndPasswordDtoTests.cs new file mode 100644 index 0000000000..24092271f7 --- /dev/null +++ b/src/SLCore.UnitTests/Common/UserAndPasswordDtoTests.cs @@ -0,0 +1,50 @@ +/* + * SonarLint for Visual Studio + * Copyright (C) 2016-2024 SonarSource SA + * mailto:info AT sonarsource DOT com + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3 of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ + +using System; +using SonarLint.VisualStudio.SLCore.Common.Models; + +namespace SonarLint.VisualStudio.SLCore.UnitTests.Common; + +[TestClass] +public class UserAndPasswordDtoTests +{ + [TestMethod] + public void Ctor_SetsPropertiesCorrectly() + { + var username = "user123"; + var password = "password123"; + + var testSubject = new UsernamePasswordDto(username, password); + + testSubject.username.Should().BeSameAs(username); + testSubject.password.Should().BeSameAs(password); + } + + [TestMethod] + public void Ctor_NullParameter_Throws() + { + Action actUsr = () => new UsernamePasswordDto(null, "password123"); + Action actPwd = () => new UsernamePasswordDto("user123", null); + + actUsr.Should().ThrowExactly(); + actPwd.Should().ThrowExactly(); + } +} diff --git a/src/SLCore.UnitTests/Listener/Credentials/GetCredentialsParamsTests.cs b/src/SLCore.UnitTests/Listener/Credentials/GetCredentialsParamsTests.cs new file mode 100644 index 0000000000..163a1e376e --- /dev/null +++ b/src/SLCore.UnitTests/Listener/Credentials/GetCredentialsParamsTests.cs @@ -0,0 +1,38 @@ +/* + * SonarLint for Visual Studio + * Copyright (C) 2016-2024 SonarSource SA + * mailto:info AT sonarsource DOT com + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3 of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ + +using Newtonsoft.Json; +using SonarLint.VisualStudio.SLCore.Listener.Credentials; + +namespace SonarLint.VisualStudio.SLCore.UnitTests.Listener.Credentials; + +[TestClass] +public class GetCredentialsParamsTests +{ + [TestMethod] + public void DeserializeObject_DeserializesCorrectly() + { + var str = """{"connectionId":"id123"}"""; + + var result = JsonConvert.DeserializeObject(str); + + result.connectionId.Should().Be("id123"); + } +} diff --git a/src/SLCore.UnitTests/Listener/Credentials/GetCredentialsResponseTests.cs b/src/SLCore.UnitTests/Listener/Credentials/GetCredentialsResponseTests.cs new file mode 100644 index 0000000000..74c5054251 --- /dev/null +++ b/src/SLCore.UnitTests/Listener/Credentials/GetCredentialsResponseTests.cs @@ -0,0 +1,96 @@ +/* + * SonarLint for Visual Studio + * Copyright (C) 2016-2024 SonarSource SA + * mailto:info AT sonarsource DOT com + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3 of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ + +using System; +using Newtonsoft.Json; +using SonarLint.VisualStudio.SLCore.Common.Models; +using SonarLint.VisualStudio.SLCore.Listener.Credentials; + +namespace SonarLint.VisualStudio.SLCore.UnitTests.Listener.Credentials; + +[TestClass] +public class GetCredentialsResponseTests +{ + [TestMethod] + public void SerializeObject_ResponseWithToken_SerializedCorrectly() + { + var testSubject = new GetCredentialsResponse(new TokenDto("token123")); + + var serializeObject = JsonConvert.SerializeObject(testSubject); + serializeObject.Should().Be("""{"credentials":{"token":"token123"}}"""); + } + + [TestMethod] + public void Ctor_ResponseWithToken_CredentialsSetCorrectly() + { + var tokenDto = new TokenDto("token123"); + var testSubject = new GetCredentialsResponse(tokenDto); + + testSubject.credentials.Left.Should().BeSameAs(tokenDto); + testSubject.credentials.Right.Should().BeNull(); + } + + [TestMethod] + public void Ctor_NullTokenDto_Throws() + { + Action act = () => new GetCredentialsResponse(token: null); + + act.Should().ThrowExactly(); + } + + [TestMethod] + public void SerializeObject_ResponseWithUserAndPassword_SerializedCorrectly() + { + var testSubject = new GetCredentialsResponse(new UsernamePasswordDto("user123", "password123")); + + var serializeObject = JsonConvert.SerializeObject(testSubject); + serializeObject.Should().Be("""{"credentials":{"username":"user123","password":"password123"}}"""); + } + + [TestMethod] + public void Ctor_ResponseWithUserAndPassword_CredentialsSetCorrectly() + { + var usernamePasswordDto = new UsernamePasswordDto("user123", "password123"); + var testSubject = new GetCredentialsResponse(usernamePasswordDto); + + testSubject.credentials.Right.Should().BeSameAs(usernamePasswordDto); + testSubject.credentials.Left.Should().BeNull(); + } + + [TestMethod] + public void Ctor_NullUserAndPasswordDto_Throws() + { + Action act = () => new GetCredentialsResponse(usernamePassword: null); + + act.Should().ThrowExactly(); + } + + [TestMethod] + public void Ctor_NoCredentials_HasNullCredentialsProperty() + { + GetCredentialsResponse.NoCredentials.credentials.Should().BeNull(); + } + + [TestMethod] + public void ResponseWithNoCredentials_SerializedCorrectly() + { + JsonConvert.SerializeObject(GetCredentialsResponse.NoCredentials).Should().Be("""{"credentials":null}"""); + } +} diff --git a/src/SLCore/Common/Models/TokenDto.cs b/src/SLCore/Common/Models/TokenDto.cs index 193c28286b..b60a89c363 100644 --- a/src/SLCore/Common/Models/TokenDto.cs +++ b/src/SLCore/Common/Models/TokenDto.cs @@ -18,13 +18,15 @@ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ +using System; + namespace SonarLint.VisualStudio.SLCore.Common.Models { public class TokenDto { public TokenDto(string token) { - this.token = token; + this.token = token ?? throw new ArgumentNullException(nameof(token)); } public string token { get; } diff --git a/src/SLCore/Common/Models/UsernamePasswordDto.cs b/src/SLCore/Common/Models/UsernamePasswordDto.cs index c89ce248fe..4834c8aaaf 100644 --- a/src/SLCore/Common/Models/UsernamePasswordDto.cs +++ b/src/SLCore/Common/Models/UsernamePasswordDto.cs @@ -18,14 +18,16 @@ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ +using System; + namespace SonarLint.VisualStudio.SLCore.Common.Models { public class UsernamePasswordDto { public UsernamePasswordDto(string username, string password) { - this.username = username; - this.password = password; + this.username = username ?? throw new ArgumentNullException(nameof(username)); + this.password = password ?? throw new ArgumentNullException(nameof(password)); } public string username { get; } diff --git a/src/SLCore/Listener/Credentials/CredentialsListener.cs b/src/SLCore/Listener/Credentials/CredentialsListener.cs index b1ffe0c1e4..8233a4cba9 100644 --- a/src/SLCore/Listener/Credentials/CredentialsListener.cs +++ b/src/SLCore/Listener/Credentials/CredentialsListener.cs @@ -20,10 +20,7 @@ using System.ComponentModel.Composition; using System.Threading.Tasks; -using Newtonsoft.Json; -using SonarLint.VisualStudio.SLCore.Common.Models; using SonarLint.VisualStudio.SLCore.Core; -using SonarLint.VisualStudio.SLCore.Protocol; namespace SonarLint.VisualStudio.SLCore.Listener.Credentials { @@ -40,35 +37,4 @@ public Task GetCredentialsAsync(GetCredentialsParams par return Task.FromResult(GetCredentialsResponse.NoCredentials); } } - - internal class GetCredentialsParams - { - public GetCredentialsParams(string connectionId) - { - this.connectionId = connectionId; - } - - public string connectionId { get; } - } - - internal class GetCredentialsResponse - { - // credentials property is nullable on the SLCore side - public static GetCredentialsResponse NoCredentials = new GetCredentialsResponse(); - - private GetCredentialsResponse(){} - - public GetCredentialsResponse(TokenDto token) - { - this.credentials = Either.CreateLeft(token); - } - - public GetCredentialsResponse(UsernamePasswordDto usernamePassword) - { - this.credentials = Either.CreateRight(usernamePassword); - } - - [JsonConverter(typeof(Either))] - public Either credentials { get; } - } } diff --git a/src/SLCore/Listener/Credentials/GetCredentialsParams.cs b/src/SLCore/Listener/Credentials/GetCredentialsParams.cs new file mode 100644 index 0000000000..526d70a87b --- /dev/null +++ b/src/SLCore/Listener/Credentials/GetCredentialsParams.cs @@ -0,0 +1,32 @@ +/* + * SonarLint for Visual Studio + * Copyright (C) 2016-2024 SonarSource SA + * mailto:info AT sonarsource DOT com + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3 of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ + +namespace SonarLint.VisualStudio.SLCore.Listener.Credentials +{ + internal class GetCredentialsParams + { + public GetCredentialsParams(string connectionId) + { + this.connectionId = connectionId; + } + + public string connectionId { get; } + } +} diff --git a/src/SLCore/Listener/Credentials/GetCredentialsResponse.cs b/src/SLCore/Listener/Credentials/GetCredentialsResponse.cs new file mode 100644 index 0000000000..6c06263239 --- /dev/null +++ b/src/SLCore/Listener/Credentials/GetCredentialsResponse.cs @@ -0,0 +1,48 @@ +/* + * SonarLint for Visual Studio + * Copyright (C) 2016-2024 SonarSource SA + * mailto:info AT sonarsource DOT com + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3 of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ + +using System; +using Newtonsoft.Json; +using SonarLint.VisualStudio.SLCore.Common.Models; +using SonarLint.VisualStudio.SLCore.Protocol; + +namespace SonarLint.VisualStudio.SLCore.Listener.Credentials +{ + internal class GetCredentialsResponse + { + // credentials property is nullable on the SLCore side + public static readonly GetCredentialsResponse NoCredentials = new GetCredentialsResponse(); + + private GetCredentialsResponse(){} + + public GetCredentialsResponse(TokenDto token) + { + credentials = Either.CreateLeft(token ?? throw new ArgumentNullException(nameof(token))); + } + + public GetCredentialsResponse(UsernamePasswordDto usernamePassword) + { + credentials = Either.CreateRight(usernamePassword ?? throw new ArgumentNullException(nameof(usernamePassword))); + } + + [JsonConverter(typeof(EitherJsonConverter))] + public Either credentials { get; } + } +}