/// <summary> /// From a call-back <paramref name="authorizationCode"/>, retrieve an /// <see cref="AccessToken"/> to query the API with. /// </summary> /// <param name="context">The context in which to execute the request.</param> /// <param name="authorizationCode">The authorisation code to create an <see cref="AccessToken"/> with.</param> /// <returns>A populated <see cref="AccessToken"/>.</returns> public async Task<AccessToken> GetAccessTokenAsync(IVisualStudioIntegrateContext context, string authorizationCode) { Guard.IsNot( authorizationCode, code => !string.IsNullOrEmpty(code), "authorizationCode", "To get an access token an authorization code must be supplied."); var request = new Request<AccessTokenDto> { Method = HttpMethod.Post, RequestUri = this._consumerApplication.AccessTokenUrl, Content = new FormUrlEncodedContent(new Dictionary<string, string> { { "grant_type", "urn:ietf:params:oauth:grant-type:jwt-bearer" }, { "assertion", authorizationCode }, { "redirect_uri", this._consumerApplication.CallbackUri.AbsoluteUri }, { "client_assertion_type", "urn:ietf:params:oauth:client-assertion-type:jwt-bearer" }, { "client_assertion", this._consumerApplication.ApplicationSecret } }) }; var response = await context.ExecuteAsync(request); var token = new AccessToken( response.AccessToken, response.ExpiresIn, this._consumerApplication.AccessTokenUrl, response.RefreshToken); return token; }
/// <summary> /// Update the access code for an expired <see cref="AccessToken"/>. /// </summary> /// <param name="context">The context in which to execute the request.</param> /// <param name="currentToken">The current identity information.</param> /// <returns>A new <see cref="AccessToken"/>.</returns> public async Task<AccessToken> RefreshAccessTokenAsync(IVisualStudioIntegrateContext context, AccessToken currentToken) { var request = new Request<AccessTokenDto> { Method = HttpMethod.Post, RequestUri = this._consumerApplication.AccessTokenUrl, Content = new FormUrlEncodedContent(new Dictionary<string, string> { { "grant_type", "refresh_token" }, { "assertion", currentToken.RefreshToken }, { "redirect_uri", this._consumerApplication.CallbackUri.AbsoluteUri }, { "client_assertion_type", "urn:ietf:params:oauth:client-assertion-type:jwt-bearer" }, { "client_assertion", this._consumerApplication.ApplicationSecret } }) }; var response = await context.ExecuteAsync(request); var refreshedToken = new AccessToken( response.AccessToken, response.ExpiresIn, this._consumerApplication.AccessTokenUrl, response.RefreshToken); return refreshedToken; }