Skip to content
This repository was archived by the owner on Oct 28, 2021. It is now read-only.

Commit 82a529b

Browse files
committed
updated code to use the new app authentication apis
1 parent 3be4c82 commit 82a529b

File tree

7 files changed

+100
-11
lines changed

7 files changed

+100
-11
lines changed

src/SymphonyOSS.RestApiClient/Api/AgentApi/MessagesApi.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ public MessagesApi(IAuthTokens authTokens, Configuration configuration, IApiExec
6161
public V2Message PostMessage(string sid, V2MessageSubmission message)
6262
{
6363
TraceSource.TraceEvent(TraceEventType.Verbose, 0, "Posting message to stream \"{0}\"", sid);
64-
return _apiExecutor.Execute(_messagesApi.V2StreamSidMessageCreatePost, sid, _authTokens.SessionToken, _authTokens.KeyManagerToken, message);
64+
return _apiExecutor.Execute(_messagesApi.V3StreamSidMessageCreatePost, sid, _authTokens.SessionToken, message, _authTokens.KeyManagerToken);
6565
}
6666

6767
/// <summary>

src/SymphonyOSS.RestApiClient/Api/PodApi/ConnectionApi.cs

+3-3
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ public class ConnectionApi
4848
new Dictionary<EventHandler<ConnectionRequestEventArgs>, Task>();
4949

5050
private Timer _connectionPollTimer;
51-
51+
5252
private event EventHandler<ConnectionRequestEventArgs> _onConnectionRequest;
5353

5454
public event EventHandler<ConnectionRequestEventArgs> OnConnectionRequest
@@ -191,9 +191,9 @@ private string GetStatusAsString(UserConnection.StatusEnum status)
191191
{
192192
switch (status)
193193
{
194-
case UserConnection.StatusEnum.PendingIncoming:
194+
case UserConnection.StatusEnum.Pendingincoming:
195195
return "pending_incoming";
196-
case UserConnection.StatusEnum.PendingOutgoing:
196+
case UserConnection.StatusEnum.Pendingoutgoing:
197197
return "pending_outgoing";
198198
default:
199199
return status.ToString().ToLower();

src/SymphonyOSS.RestApiClient/Api/PodApi/UserApi.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ public SuccessResponse ResetPassword(long uid)
9090
_userApi.V1AdminUserUidActionPasswordResetPost,
9191
_authTokens.SessionToken,
9292
(long?)uid,
93-
new PasswordReset(PasswordReset.TypeEnum.Email));
93+
new PasswordReset(PasswordReset.TypeEnum.EMAIL));
9494
}
9595

9696
/// <summary>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
// Licensed to the Symphony Software Foundation (SSF) under one
2+
// or more contributor license agreements. See the NOTICE file
3+
// distributed with this work for additional information
4+
// regarding copyright ownership. The SSF licenses this file
5+
// to you under the Apache License, Version 2.0 (the
6+
// "License"); you may not use this file except in compliance
7+
// with the License. You may obtain a copy of the License at
8+
//
9+
// http://www.apache.org/licenses/LICENSE-2.0
10+
//
11+
// Unless required by applicable law or agreed to in writing,
12+
// software distributed under the License is distributed on an
13+
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
// KIND, either express or implied. See the License for the
15+
// specific language governing permissions and limitations
16+
// under the License.
17+
18+
namespace SymphonyOSS.RestApiClient.Authentication
19+
{
20+
using System.Security.Cryptography.X509Certificates;
21+
using Factories;
22+
using Generated.OpenApi.AuthenticatorApi.Api;
23+
24+
/// <summary>
25+
/// Contains the session token needed for an application to authenticate on behalf of a user,
26+
/// and the logic for generating this token using the authentication endpoints.
27+
/// </summary>
28+
public class AppSessionManager : ISessionManager
29+
{
30+
private readonly IAuthenticationApi _sessionAuthApi;
31+
32+
private string _appSessionToken;
33+
private string _userSessionToken;
34+
private string _username;
35+
36+
public AppSessionManager(string sessionAuthUrl, X509Certificate2 appCertificate, string username)
37+
{
38+
Certificate = appCertificate;
39+
_username = username;
40+
41+
var sessionAuthApiFactory = new AuthenticatorApiFactory(sessionAuthUrl);
42+
_sessionAuthApi = sessionAuthApiFactory.CreateAuthenticationApi(appCertificate);
43+
}
44+
45+
public AppSessionManager(IAuthenticationApi sessionAuthApi, IAuthenticationApi keyAuthApi, X509Certificate2 certificate)
46+
{
47+
Certificate = certificate;
48+
_sessionAuthApi = sessionAuthApi;
49+
}
50+
51+
public X509Certificate2 Certificate { get; }
52+
53+
public string SessionToken
54+
{
55+
get
56+
{
57+
if (_userSessionToken == null)
58+
{
59+
GenerateTokens();
60+
}
61+
62+
return _appSessionToken;
63+
}
64+
}
65+
66+
public string KeyManagerToken
67+
{
68+
get
69+
{
70+
return null;
71+
}
72+
}
73+
74+
/// <summary>
75+
/// Generates both the session and key manager tokens.
76+
/// </summary>
77+
public void GenerateTokens()
78+
{
79+
// This could be made more efficient by reusing the cached appSessionToken and
80+
// only regenerating it if the call to V1AppUsernameUsernameAuthenticatePost fails.
81+
82+
_appSessionToken = _sessionAuthApi.V1AppAuthenticatePost()._Token;
83+
_userSessionToken = _sessionAuthApi.V1AppUsernameUsernameAuthenticatePost(_username, _appSessionToken)._Token;
84+
85+
}
86+
87+
}
88+
}

src/SymphonyOSS.RestApiClient/SymphonyOSS.RestApiClient.csproj

+1
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@
6666
<Compile Include="Api\PodApi\MessageSuppressionApi.cs" />
6767
<Compile Include="Api\PodApi\SessionApi.cs" />
6868
<Compile Include="Api\PodApi\SystemApi.cs" />
69+
<Compile Include="Authentication\AppSessionManager.cs" />
6970
<Compile Include="Authentication\ISessionManager.cs" />
7071
<Compile Include="Authentication\IAuthTokens.cs" />
7172
<Compile Include="Authentication\SessionManager.cs" />

test/SymphonyOSS.RestApiClient.Tests/MessagesApiTest.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -48,15 +48,15 @@ public void EnsurePostMessage_uses_retry_strategy()
4848
const string sid = "sid";
4949
var message = new V2MessageSubmission();
5050
_messagesApi.PostMessage(sid, message);
51-
_apiExecutorMock.Verify(obj => obj.Execute(It.IsAny<Func<string, string, string, V2MessageSubmission, V2Message>>(), sid, "sessionToken", "keyManagerToken", message));
51+
_apiExecutorMock.Verify(obj => obj.Execute(It.IsAny<Func<string, string, V2MessageSubmission, string, V2Message>>(), sid, "sessionToken", message, "keyManagerToken"));
5252
}
5353

5454
[Fact]
5555
public void EnsureGet_uses_retry_strategy()
5656
{
5757
const string sid = "sid";
5858
_messagesApi.GetMessages(sid, null);
59-
_apiExecutorMock.Verify(obj => obj.Execute(It.IsAny<Func<string, long?, string, string, int?, int?, V2MessageList>>(), sid, (long?) null, "sessionToken", "keyManagerToken", (int?) null, (int?) null));
59+
_apiExecutorMock.Verify(obj => obj.Execute(It.IsAny<Func<string, long?, string, string, int?, int?, V2MessageList>>(), sid, (long?)null, "sessionToken", "keyManagerToken", (int?)null, (int?)null));
6060
}
6161
}
6262
}

test/SymphonyOSS.RestApiClient.Tests/SecurityApiTest.cs

+4-4
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,8 @@ public void EnsureCreate_uses_api_executor()
4949
"pem",
5050
new CompanyCertAttributes(
5151
"name",
52-
new CompanyCertType(CompanyCertType.TypeEnum.Operationssigning),
53-
new CompanyCertStatus(CompanyCertStatus.TypeEnum.Trusted)));
52+
new CompanyCertType(CompanyCertType.TypeEnum.OPERATIONSSIGNING),
53+
new CompanyCertStatus(CompanyCertStatus.TypeEnum.TRUSTED)));
5454
_securityApi.Create(cert);
5555
_apiExecutorMock.Verify(obj => obj.Execute(It.IsAny<Func<string, CompanyCert, SuccessResponse>>(), "sessionToken", cert));
5656
}
@@ -81,8 +81,8 @@ public void EnsureUpdate_uses_api_executor()
8181
{
8282
var certAttributes = new CompanyCertAttributes(
8383
"name",
84-
new CompanyCertType(CompanyCertType.TypeEnum.Operationssigning),
85-
new CompanyCertStatus(CompanyCertStatus.TypeEnum.Trusted));
84+
new CompanyCertType(CompanyCertType.TypeEnum.OPERATIONSSIGNING),
85+
new CompanyCertStatus(CompanyCertStatus.TypeEnum.TRUSTED));
8686
_securityApi.Update("fingerPrint", certAttributes);
8787
_apiExecutorMock.Verify(obj => obj.Execute(It.IsAny<Func<string, string, CompanyCertAttributes, SuccessResponse>>(), "fingerPrint", "sessionToken", certAttributes));
8888
}

0 commit comments

Comments
 (0)