Release 4.0.0-preview1-00051
[platform/core/csapi/tizenfx.git] / src / Tizen.Account.OAuth2 / Tizen.Account.OAuth2 / TokenResponse.cs
1 /*
2  * Copyright (c) 2016 Samsung Electronics Co., Ltd All Rights Reserved
3  *
4  * Licensed under the Apache License, Version 2.0 (the License);
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an AS IS BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 using System;
18 using System.Runtime.InteropServices;
19
20 namespace Tizen.Account.OAuth2
21 {
22     /// <summary>
23     /// The response from authroization server containing access token and an optional refresh token.
24     /// </summary>
25     /// <since_tizen> 3 </since_tizen>
26     public class TokenResponse
27     {
28         private bool _disposed = false;
29         private IntPtr _responseHandle;
30
31         internal TokenResponse(IntPtr handle)
32         {
33             _responseHandle = handle;
34         }
35
36         /// <summary>
37         /// Destructor of the AuthorizationResponse class.
38         /// </summary>
39         /// <since_tizen> 3 </since_tizen>
40         ~TokenResponse()
41         {
42             Dispose(false);
43         }
44
45         /// <summary>
46         /// The access token
47         /// </summary>
48         /// <since_tizen> 3 </since_tizen>
49         public AccessToken AccessToken { get; internal set; }
50
51         /// <summary>
52         /// The state parameter present in authorization request.
53         /// </summary>
54         /// <since_tizen> 3 </since_tizen>
55         /// <remarks>
56         /// The value can be null depending on the server specifications.
57         /// </remarks>
58         public string State { get; internal set; }
59
60         /// <summary>
61         /// The refresh token. The value will be null if authorization server doesn't return a refresh token.
62         /// </summary>
63         /// <since_tizen> 3 </since_tizen>
64         /// <remarks>
65         /// Issuing a refresh token is optional at the discretion of the authorization server.
66         /// </remarks>
67         public RefreshToken RefreshToken { get; internal set; }
68
69         /// <summary>
70         /// Gets the value of the key received from service provider
71         /// </summary>
72         /// <since_tizen> 3 </since_tizen>
73         /// <returns>The value of respecitve key </returns>
74         /// <exception cref="System.ArgumentException">Thrown when the key does not exist or when there is an invalid parameter.</exception>
75         public string GetCustomValue(string key)
76         {
77             IntPtr value;
78             int ret = Interop.Response.GetCustomData(_responseHandle, key, out value);
79             if (ret != (int)OAuth2Error.None)
80             {
81                 Log.Error(ErrorFactory.LogTag, "Interop failed");
82                 throw ErrorFactory.GetException(ret);
83             }
84             return Marshal.PtrToStringAnsi(value);
85         }
86
87         /// <summary>
88         /// Releases any unmanaged resources used by this object.
89         /// </summary>
90         /// <since_tizen> 3 </since_tizen>
91         public void Dispose()
92         {
93             Dispose(true);
94             GC.SuppressFinalize(this);
95         }
96
97         /// <summary>
98         /// Releases any unmanaged resources used by this object. Can also dispose any other disposable objects.
99         /// </summary>
100         /// <since_tizen> 3 </since_tizen>
101         /// <param name="disposing">If true, disposes any disposable objects. If false, does not dispose disposable objects.</param>
102         protected virtual void Dispose(bool disposing)
103         {
104             if (_disposed)
105                 return;
106
107             if (disposing)
108             {
109                 // Free managed objects
110             }
111
112             Interop.Response.Destroy(_responseHandle);
113             _disposed = true;
114         }
115     }
116 }