/// <inheritdoc /> protected override async Task EnsureAuthenticatedAsync(HttpRequestMessage request, CancellationToken cancellationToken) { // Authenticate? if (AuthenticationTokens == null || AuthenticationTokens.HasExpired()) { // Get new token var spaceTokenRequest = new HttpRequestMessage(HttpMethod.Post, ServerUrl + "oauth/token") { Headers = { Authorization = AuthenticationHeaderValue.Parse( "Basic " + Convert.ToBase64String(Encoding.UTF8.GetBytes($"{_clientId}:{_clientSecret}"))) }, Content = new FormUrlEncodedContent(new [] { new KeyValuePair <string, string>("grant_type", "client_credentials"), new KeyValuePair <string, string>("scope", Scope) }) }; var spaceTokenResponse = await HttpClient.SendAsync(spaceTokenRequest, cancellationToken); if (!spaceTokenResponse.IsSuccessStatusCode) { throw new ResourceException($"Unable to connect to Space organization. Attempted endpoint was: {ServerUrl + "oauth/token"}", spaceTokenResponse.StatusCode, spaceTokenResponse.ReasonPhrase); } using var spaceTokenDocument = await JsonDocument.ParseAsync(await spaceTokenResponse.Content.ReadAsStreamAsync(), cancellationToken : cancellationToken); var spaceToken = spaceTokenDocument.RootElement; AuthenticationTokens = new AuthenticationTokens( accessToken: spaceToken.GetStringValue("access_token"), refreshToken: spaceToken.GetStringValue("refresh_token") ?? AuthenticationTokens?.RefreshToken, expires: DateTimeOffset.UtcNow.AddSeconds(spaceToken.GetInt32Value("expires_in")) ); } await base.EnsureAuthenticatedAsync(request, cancellationToken); }
/// <inheritdoc /> protected override async Task EnsureAuthenticatedAsync(HttpRequestMessage request, CancellationToken cancellationToken) { // Authenticate? if (AuthenticationTokens != null && AuthenticationTokens.HasExpired() && !string.IsNullOrEmpty(AuthenticationTokens.RefreshToken)) { // Get new token var spaceTokenRequest = new HttpRequestMessage(HttpMethod.Post, ServerUrl + "oauth/token") { Headers = { Authorization = AuthenticationHeaderValue.Parse( "Basic " + Convert.ToBase64String(Encoding.UTF8.GetBytes($"{_clientId}:{_clientSecret}"))) }, Content = new FormUrlEncodedContent(new [] { new KeyValuePair <string, string>("grant_type", "refresh_token"), new KeyValuePair <string, string>("refresh_token", AuthenticationTokens.RefreshToken), new KeyValuePair <string, string>("scope", Scope) }) }; var spaceTokenResponse = await HttpClient.SendAsync(spaceTokenRequest, cancellationToken); using var spaceTokenDocument = await JsonDocument.ParseAsync(await spaceTokenResponse.Content.ReadAsStreamAsync(), cancellationToken : cancellationToken); var spaceToken = spaceTokenDocument.RootElement; AuthenticationTokens = new AuthenticationTokens( accessToken: spaceToken.GetStringValue("access_token"), refreshToken: spaceToken.GetStringValue("refresh_token") ?? AuthenticationTokens.RefreshToken, expires: DateTimeOffset.UtcNow.AddSeconds(spaceToken.GetInt32Value("expires_in")) ); } await base.EnsureAuthenticatedAsync(request, cancellationToken); }
/// <summary> /// Creates an instance of the <see cref="RefreshTokenConnection" /> class. /// </summary> /// <param name="serverUrl">Space organization URL that will be connected against.</param> /// <param name="clientId">The client id to use when refreshing tokens.</param> /// <param name="clientSecret">The client secret to use when refreshing tokens.</param> /// <param name="authenticationTokens">Authentication tokens to use while authenticating.</param> /// <param name="httpClient">HTTP client to use for communication.</param> public RefreshTokenConnection(Uri serverUrl, string clientId, string clientSecret, AuthenticationTokens authenticationTokens, HttpClient?httpClient = null) : base(serverUrl, authenticationTokens, httpClient) { if (string.IsNullOrEmpty(authenticationTokens.RefreshToken)) { throw new ArgumentException("The authentication tokens do not contain a valid refresh token. Make sure the refresh token is not null or an empty string.", nameof(authenticationTokens)); } _clientId = clientId; _clientSecret = clientSecret; }
/// <summary> /// Creates an instance of the <see cref="BearerTokenConnection" /> class. /// </summary> /// <param name="serverUrl">Space organization URL that will be connected against.</param> /// <param name="authenticationTokens">Authentication tokens to use.</param> /// <param name="httpClient">HTTP client to use for communication.</param> public BearerTokenConnection(Uri serverUrl, AuthenticationTokens authenticationTokens, HttpClient?httpClient = null) : base(serverUrl) { AuthenticationTokens = authenticationTokens; HttpClient = httpClient ?? new HttpClient(); }