|
| 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 | +} |
0 commit comments