Release 4.0.0-preview1-00051
[platform/core/csapi/tizenfx.git] / src / Tizen.Account.OAuth2 / Tizen.Account.OAuth2 / AuthorizationResponse.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 containing authroization code from the authorization server.
24     /// </summary>
25     /// <since_tizen> 3 </since_tizen>
26     public class AuthorizationResponse : IDisposable
27     {
28         private bool _disposed = false;
29         private IntPtr _responseHandle;
30
31         internal AuthorizationResponse(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         ~AuthorizationResponse()
41         {
42             Dispose(false);
43         }
44
45         /// <summary>
46         /// The authroization code.
47         /// </summary>
48         /// <since_tizen> 3 </since_tizen>
49         public string Code { 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         /// Custom key-value parameter received from service provider
62         /// </summary>
63         /// <since_tizen> 3 </since_tizen>
64         /// <remarks>
65         /// The return value can be null depending on the server specifications.
66         /// </remarks>
67         public string GetCustomValue(string key)
68         {
69             IntPtr value = IntPtr.Zero;
70             int ret = Interop.Response.GetCustomData(_responseHandle, key, out value);
71             if (ret != (int)OAuth2Error.None)
72             {
73                 Log.Error(ErrorFactory.LogTag, "Interop failed");
74                 throw ErrorFactory.GetException(ret);
75             }
76             return Marshal.PtrToStringAnsi(value);
77         }
78
79         /// <summary>
80         /// Releases any unmanaged resources used by this object.
81         /// </summary>
82         /// <since_tizen> 3 </since_tizen>
83         public void Dispose()
84         {
85             Dispose(true);
86             GC.SuppressFinalize(this);
87         }
88
89         /// <summary>
90         /// Releases any unmanaged resources used by this object. Can also dispose any other disposable objects.
91         /// </summary>
92         /// <since_tizen> 3 </since_tizen>
93         /// <param name="disposing">If true, disposes any disposable objects. If false, does not dispose disposable objects.</param>
94         protected virtual void Dispose(bool disposing)
95         {
96             if (_disposed)
97                 return;
98
99             if (disposing)
100             {
101                 // Free managed objects
102             }
103
104             Interop.Response.Destroy(_responseHandle);
105             _disposed = true;
106         }
107     }
108 }