1 //----------------------------------------------------------------------
3 // Copyright (c) Microsoft Corporation.
4 // All rights reserved.
6 // This code is licensed under the MIT License.
8 // Permission is hereby granted, free of charge, to any person obtaining a copy
9 // of this software and associated documentation files(the "Software"), to deal
10 // in the Software without restriction, including without limitation the rights
11 // to use, copy, modify, merge, publish, distribute, sublicense, and / or sell
12 // copies of the Software, and to permit persons to whom the Software is
13 // furnished to do so, subject to the following conditions :
15 // The above copyright notice and this permission notice shall be included in
16 // all copies or substantial portions of the Software.
18 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.IN NO EVENT SHALL THE
21 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
26 //------------------------------------------------------------------------------
30 namespace Microsoft.IdentityModel.Clients.ActiveDirectory
32 class AcquireTokenByAuthorizationCodeHandler : AcquireTokenHandlerBase
34 private readonly string authorizationCode;
36 private readonly Uri redirectUri;
38 public AcquireTokenByAuthorizationCodeHandler(RequestData requestData, string authorizationCode, Uri redirectUri)
41 if (requestData.Resource == null)
43 requestData.Resource = NullResource;
46 if (string.IsNullOrWhiteSpace(authorizationCode))
48 throw new ArgumentNullException("authorizationCode");
51 this.authorizationCode = authorizationCode;
53 if (redirectUri == null)
55 throw new ArgumentNullException("redirectUri");
58 this.redirectUri = redirectUri;
60 this.LoadFromCache = false;
62 this.SupportADFS = true;
65 protected override void AddAditionalRequestParameters(DictionaryRequestParameters requestParameters)
67 requestParameters[OAuthParameter.GrantType] = OAuthGrantType.AuthorizationCode;
68 requestParameters[OAuthParameter.Code] = this.authorizationCode;
69 requestParameters[OAuthParameter.RedirectUri] = this.redirectUri.OriginalString;
72 protected override void PostTokenRequest(AuthenticationResultEx resultEx)
74 base.PostTokenRequest(resultEx);
75 UserInfo userInfo = resultEx.Result.UserInfo;
76 this.UniqueId = (userInfo == null) ? null : userInfo.UniqueId;
77 this.DisplayableId = (userInfo == null) ? null : userInfo.DisplayableId;
78 if (resultEx.ResourceInResponse != null)
80 this.Resource = resultEx.ResourceInResponse;
81 PlatformPlugin.Logger.Verbose(this.CallState, "Resource value in the token response was used for storing tokens in the cache");
84 // If resource is not passed as an argument and is not returned by STS either,
85 // we cannot store the token in the cache with null resource.
86 // TODO: Store refresh token though if STS supports MRRT.
87 this.StoreToCache = this.StoreToCache && (this.Resource != null);