[Tizen] Add BuildTools 2.1.0-rc1-02804-05
[platform/upstream/coreclr.git] / Tools / dotnetcli / sdk / NuGetFallbackFolder / microsoft.identitymodel.clients.activedirectory / 3.14.1 / src / src / ADAL.PCL / Flows / AcquireTokenByAuthorizationCodeHandler.cs
1 //----------------------------------------------------------------------
2 //
3 // Copyright (c) Microsoft Corporation.
4 // All rights reserved.
5 //
6 // This code is licensed under the MIT License.
7 //
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 :
14 //
15 // The above copyright notice and this permission notice shall be included in
16 // all copies or substantial portions of the Software.
17 //
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
24 // THE SOFTWARE.
25 //
26 //------------------------------------------------------------------------------
27
28 using System;
29
30 namespace Microsoft.IdentityModel.Clients.ActiveDirectory
31 {
32     class AcquireTokenByAuthorizationCodeHandler : AcquireTokenHandlerBase
33     {
34         private readonly string authorizationCode;
35
36         private readonly Uri redirectUri;
37
38         public AcquireTokenByAuthorizationCodeHandler(RequestData requestData, string authorizationCode, Uri redirectUri)
39             : base(requestData)
40         {
41             if (requestData.Resource == null)
42             {
43                 requestData.Resource = NullResource;
44             }
45
46             if (string.IsNullOrWhiteSpace(authorizationCode))
47             {
48                 throw new ArgumentNullException("authorizationCode");
49             }
50
51             this.authorizationCode = authorizationCode;
52
53             if (redirectUri == null)
54             {
55                 throw new ArgumentNullException("redirectUri");
56             }
57
58             this.redirectUri = redirectUri;
59
60             this.LoadFromCache = false;
61
62             this.SupportADFS = true;
63         }
64
65         protected override void AddAditionalRequestParameters(DictionaryRequestParameters requestParameters)
66         {
67             requestParameters[OAuthParameter.GrantType] = OAuthGrantType.AuthorizationCode;
68             requestParameters[OAuthParameter.Code] = this.authorizationCode;
69             requestParameters[OAuthParameter.RedirectUri] = this.redirectUri.OriginalString;
70         }
71
72         protected override void PostTokenRequest(AuthenticationResultEx resultEx)
73         {
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)
79             {
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");
82             }
83
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);
88         }
89     }
90 }