1- using System ;
1+ using System ;
22using System . Collections . Generic ;
33using System . Linq ;
44using System . Text . Json ;
1111using Certify . Models . Hub ;
1212using Certify . Shared ;
1313using Certify . Shared . Core . Utils ;
14+ using Microsoft . IdentityModel . JsonWebTokens ;
1415
1516namespace Certify . Management
1617{
@@ -21,6 +22,7 @@ public partial class CertifyManager
2122 private bool _isHubConnectionErrorLogged = false ;
2223 private ClientSecret _mgmtHubJoiningSecret ;
2324 private const string _mgmtHubJoiningCredId = "_ManagementHubJoiningKey" ;
25+ private string _mgmtHubJoiningToken = default ! ;
2426
2527 public async Task < ActionResult > CheckManagementHubConnectionStatus ( )
2628 {
@@ -40,6 +42,8 @@ public async Task<ActionResult> JoinManagementHub(string url, ClientSecret clien
4042
4143 if ( check . IsSuccess )
4244 {
45+ _mgmtHubJoiningToken = check . Result . JoiningToken ;
46+
4347 _serverConfig = SharedUtils . ServiceConfigManager . GetAppServiceConfig ( ) ;
4448 var hubEndpoint = check . Result . HubEndpoint ;
4549
@@ -119,32 +123,84 @@ public void SetDirectManagementClient(IManagementServerClient client)
119123 _managementServerClient = client ;
120124 }
121125
126+ private JsonWebTokenHandler _joiningTokenHandler = new JsonWebTokenHandler ( ) ;
122127 private async Task EnsureMgmtHubConnection ( )
123128 {
129+ // check we have a current non-expired joining token
130+ if ( ! string . IsNullOrWhiteSpace ( _mgmtHubJoiningToken ) )
131+ {
132+ // check jwt has not expired
133+
134+ var validation = await _joiningTokenHandler . ValidateTokenAsync ( _mgmtHubJoiningToken , new Microsoft . IdentityModel . Tokens . TokenValidationParameters
135+ {
136+ ValidateLifetime = true ,
137+ ValidateAudience = false ,
138+ ValidateIssuer = false ,
139+ ValidateIssuerSigningKey = false
140+ } ) ;
141+
142+ if ( ! validation . IsValid )
143+ {
144+ // token has expired, will need a new one
145+ _mgmtHubJoiningToken = null ;
146+ }
147+ }
148+
124149 // connect/reconnect to management hub if enabled
125150 if ( _managementServerClient == null || ! _managementServerClient . IsConnected ( ) )
126151 {
127152 var mgmtHubUri = string . Empty ;
153+ var api = string . Empty ;
154+ var endpoint = string . Empty ;
155+ var defaultEnpoint = "api/internal/managementhub" ;
128156
129157 // construct hub api url and status hub api endpoint
130158 if ( Environment . GetEnvironmentVariable ( "CERTIFY_MANAGEMENT_HUB" ) != null )
131159 {
132- var api = Environment . GetEnvironmentVariable ( "CERTIFY_MANAGEMENT_HUB" ) ;
160+ api = Environment . GetEnvironmentVariable ( "CERTIFY_MANAGEMENT_HUB" ) ;
133161
134- if ( api . EndsWith ( "api/internal/managementhub" ) )
162+ if ( api . EndsWith ( defaultEnpoint ) )
135163 {
136164 mgmtHubUri = api ;
165+
166+ endpoint = defaultEnpoint ;
167+ api = api . Replace ( defaultEnpoint , "" ) ;
137168 }
138169 else
139170 {
140- var endpoint = Environment . GetEnvironmentVariable ( "CERTIFY_MANAGEMENT_HUB_ENDPOINT" ) ?? "api/internal/managementhub" ;
171+ endpoint = Environment . GetEnvironmentVariable ( "CERTIFY_MANAGEMENT_HUB_ENDPOINT" ) ?? defaultEnpoint ;
141172 mgmtHubUri = $ "{ api . Trim ( '/' ) } /{ endpoint . Trim ( '/' ) } ";
142173 }
143174 }
144175 else
145176 {
146- mgmtHubUri = $ "{ _serverConfig . ManagementServerHubAPI . Trim ( '/' ) } /{ _serverConfig . ManagementServerHubEndpoint . Trim ( '/' ) } ";
177+ api = _serverConfig . ManagementServerHubAPI . Trim ( '/' ) ;
178+ endpoint = _serverConfig . ManagementServerHubEndpoint . Trim ( '/' ) ;
179+ mgmtHubUri = $ "{ api } /{ endpoint } ";
180+ }
181+
182+ if ( string . IsNullOrWhiteSpace ( _mgmtHubJoiningToken ) )
183+ {
184+ if ( _mgmtHubJoiningSecret == null )
185+ {
186+ var secret = await _credentialsManager . GetUnlockedCredential ( _mgmtHubJoiningCredId ) ;
187+ if ( secret != null )
188+ {
189+ _mgmtHubJoiningSecret = JsonSerializer . Deserialize < ClientSecret > ( secret , JsonOptions . DefaultJsonSerializerOptions ) ;
190+ }
191+ }
147192
193+ // acquire new token
194+ var check = await CheckManagementHubCredentials ( api , _mgmtHubJoiningSecret ) ;
195+ if ( check . IsSuccess )
196+ {
197+ _mgmtHubJoiningToken = check . Result . JoiningToken ;
198+ }
199+ else
200+ {
201+ _serviceLog . Error ( $ "Failed to acquire new hub joining token using current joining key: { check . Message } ") ;
202+ return ;
203+ }
148204 }
149205
150206 if ( ! string . IsNullOrWhiteSpace ( mgmtHubUri ) )
@@ -192,16 +248,8 @@ private async Task StartManagementHubConnection(string hubUri)
192248 _managementServerClient . OnConnectionReconnecting -= _managementServerClient_OnConnectionReconnecting ;
193249 }
194250
195- if ( _mgmtHubJoiningSecret == null )
196- {
197- var secret = await _credentialsManager . GetUnlockedCredential ( _mgmtHubJoiningCredId ) ;
198- if ( secret != null )
199- {
200- _mgmtHubJoiningSecret = JsonSerializer . Deserialize < ClientSecret > ( secret , JsonOptions . DefaultJsonSerializerOptions ) ;
201- }
202- }
203-
204- _managementServerClient = new ManagementServerClient ( hubUri , _mgmtHubJoiningSecret , instanceInfo ) ;
251+ _managementServerClient = new ManagementServerClient ( hubUri , instanceInfo ) ;
252+ _managementServerClient . SetJoiningToken ( _mgmtHubJoiningToken ) ;
205253
206254 try
207255 {
0 commit comments