[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 / AuthenticationResultEx.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 using System.Collections.Generic;
30 using System.IO;
31 using System.Linq;
32 using System.Runtime.Serialization;
33 using System.Runtime.Serialization.Json;
34 using System.Security;
35 using System.Text;
36 using System.Threading.Tasks;
37
38 namespace Microsoft.IdentityModel.Clients.ActiveDirectory
39 {
40     [DataContract]
41     internal class AuthenticationResultEx
42     {
43         [DataMember]
44         public AuthenticationResult Result { get; set; }
45
46         /// <summary>
47         /// Gets the Refresh Token associated with the requested Access Token. Note: not all operations will return a Refresh Token.
48         /// </summary>
49         [DataMember]
50         public string RefreshToken { get; set; }
51
52         /// <summary>
53         /// Gets a value indicating whether the refresh token can be used for requesting access token for other resources.
54         /// </summary>
55         internal bool IsMultipleResourceRefreshToken
56         {
57             get
58             {
59                 return (!string.IsNullOrWhiteSpace(this.RefreshToken) && !string.IsNullOrWhiteSpace(this.ResourceInResponse));
60             }            
61         }
62
63         // This is only needed for AcquireTokenByAuthorizationCode in which parameter resource is optional and we need
64         // to get it from the STS response.
65         [DataMember]
66         internal string ResourceInResponse { get; set; }
67
68
69         /// <summary>
70         /// Serializes the object to a JSON string
71         /// </summary>
72         /// <returns>Deserialized authentication result</returns>
73         public static AuthenticationResultEx Deserialize(string serializedObject)
74         {
75             AuthenticationResultEx resultEx;
76             var serializer = new DataContractJsonSerializer(typeof(AuthenticationResultEx));
77             byte[] serializedObjectBytes = Encoding.UTF8.GetBytes(serializedObject);
78             using (var stream = new MemoryStream(serializedObjectBytes))
79             {
80                 resultEx = (AuthenticationResultEx)serializer.ReadObject(stream);
81             }
82
83             return resultEx;
84         }
85
86         /// <summary>
87         /// Serializes the object to a JSON string
88         /// </summary>
89         /// <returns>Serialized authentication result</returns>
90         public string Serialize()
91         {
92             string serializedObject;
93             var serializer = new DataContractJsonSerializer(typeof(AuthenticationResultEx));
94             using (MemoryStream stream = new MemoryStream())
95             {
96                 serializer.WriteObject(stream, this);
97                 serializedObject = Encoding.UTF8.GetString(stream.ToArray(), 0, (int)stream.Position);
98             }
99
100             return serializedObject;
101         }
102
103         internal Exception Exception { get; set; }
104         
105         [DataMember]
106         public string UserAssertionHash { get; set; }
107
108         internal AuthenticationResultEx Clone()
109         {
110             return new AuthenticationResultEx
111             {
112                 UserAssertionHash = this.UserAssertionHash,
113                 Exception = this.Exception,
114                 RefreshToken = this.RefreshToken,
115                 ResourceInResponse = this.ResourceInResponse,
116                 Result = new AuthenticationResult(this.Result.AccessTokenType, this.Result.AccessToken, this.Result.ExpiresOn, this.Result.ExtendedExpiresOn)
117                 {
118                     ExtendedLifeTimeToken = this.Result.ExtendedLifeTimeToken,
119                     IdToken = this.Result.IdToken,
120                     TenantId = this.Result.TenantId,
121                     UserInfo = new UserInfo(this.Result.UserInfo)
122                 }
123             };
124         }
125     }
126 }