1
+ /**
2
+ * Copyright 2019 IBM Corp. 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
+
18
+ using IBM . Cloud . SDK . Connection ;
19
+ using IBM . Cloud . SDK . Utilities ;
20
+ using System ;
21
+ using System . Collections . Generic ;
22
+ using UnityEngine . Networking ;
23
+ using Newtonsoft . Json ;
24
+ using System . Text ;
25
+ using Utility = IBM . Cloud . SDK . Utilities . Utility ;
26
+
27
+
28
+ namespace IBM . Cloud . SDK . Authentication
29
+ {
30
+ public class IamTokenManager : JwtTokenManager
31
+ {
32
+ private string iamApikey ;
33
+ private string iamClientId ;
34
+ private string iamClientSecret ;
35
+ private string iamDefaultUrl = "https://iam.cloud.ibm.com/identity/token" ;
36
+
37
+ private const string CLIENT_ID_SECRET_WARNING = "Warning: Client ID and Secret must BOTH be given, or the defaults will be used." ;
38
+
39
+ public IamTokenManager ( IamTokenOptions options ) : base ( options )
40
+ {
41
+ if ( string . IsNullOrEmpty ( url ) )
42
+ {
43
+ if ( ! string . IsNullOrEmpty ( options . IamUrl ) )
44
+ {
45
+ url = options . IamUrl ;
46
+ }
47
+ else
48
+ {
49
+ url = iamDefaultUrl ;
50
+ }
51
+ }
52
+
53
+ if ( ! string . IsNullOrEmpty ( options . IamApiKey ) )
54
+ {
55
+ iamApikey = options . IamApiKey ;
56
+ }
57
+
58
+ if ( ! string . IsNullOrEmpty ( options . IamAccessToken ) )
59
+ {
60
+ userAccessToken = options . IamAccessToken ;
61
+ }
62
+
63
+ if ( ! string . IsNullOrEmpty ( options . IamClientId ) )
64
+ {
65
+ iamClientId = options . IamClientId ;
66
+ }
67
+
68
+ if ( ! string . IsNullOrEmpty ( options . IamClientSecret ) )
69
+ {
70
+ iamClientSecret = options . IamClientSecret ;
71
+ }
72
+
73
+ if ( string . IsNullOrEmpty ( options . IamClientSecret ) || string . IsNullOrEmpty ( options . IamClientId ) )
74
+ {
75
+ iamClientId = "bx" ;
76
+ iamClientSecret = "bx" ;
77
+ Log . Warning ( "IamTokenManager():" , CLIENT_ID_SECRET_WARNING ) ;
78
+ }
79
+ }
80
+
81
+ public void SetIamAuthorizationInfo ( string IamClientId , string IamClientSecret )
82
+ {
83
+ iamClientId = IamClientId ;
84
+ iamClientSecret = IamClientSecret ;
85
+ if ( string . IsNullOrEmpty ( iamClientSecret ) || string . IsNullOrEmpty ( iamClientId ) )
86
+ {
87
+ Log . Warning ( "SetIamAuthorizationInfo():" , CLIENT_ID_SECRET_WARNING ) ;
88
+ }
89
+ }
90
+
91
+ #region Request Token
92
+ /// <summary>
93
+ /// Request an IAM token using an API key.
94
+ /// </summary>
95
+ /// <param name="callback">The request callback.</param>
96
+ /// <param name="error"> The request error.</param>
97
+ /// <returns></returns>
98
+ override protected bool RequestToken ( Callback < TokenData > callback )
99
+ {
100
+ if ( callback == null )
101
+ throw new ArgumentNullException ( "successCallback" ) ;
102
+
103
+ RESTConnector connector = new RESTConnector ( ) ;
104
+ connector . URL = url ;
105
+ if ( connector == null )
106
+ return false ;
107
+
108
+ RequestIamTokenRequest req = new RequestIamTokenRequest ( ) ;
109
+ req . Callback = callback ;
110
+ req . HttpMethod = UnityWebRequest . kHttpVerbGET ;
111
+ req . Headers . Add ( "Content-type" , "application/x-www-form-urlencoded" ) ;
112
+ req . Headers . Add ( "Authorization" , Utility . CreateAuthorization ( iamClientId , iamClientSecret ) ) ;
113
+ req . OnResponse = OnRequestIamTokenResponse ;
114
+ req . DisableSslVerification = disableSslVerification ;
115
+ req . Forms = new Dictionary < string , RESTConnector . Form > ( ) ;
116
+ req . Forms [ "grant_type" ] = new RESTConnector . Form ( "urn:ibm:params:oauth:grant-type:apikey" ) ;
117
+ req . Forms [ "apikey" ] = new RESTConnector . Form ( iamApikey ) ;
118
+ req . Forms [ "response_type" ] = new RESTConnector . Form ( "cloud_iam" ) ;
119
+
120
+ return connector . Send ( req ) ;
121
+ }
122
+
123
+ private class RequestIamTokenRequest : RESTConnector . Request
124
+ {
125
+ public Callback < TokenData > Callback { get ; set ; }
126
+ }
127
+
128
+ private void OnRequestIamTokenResponse ( RESTConnector . Request req , RESTConnector . Response resp )
129
+ {
130
+ DetailedResponse < TokenData > response = new DetailedResponse < TokenData > ( ) ;
131
+ response . Result = new TokenData ( ) ;
132
+ foreach ( KeyValuePair < string , string > kvp in resp . Headers )
133
+ {
134
+ response . Headers . Add ( kvp . Key , kvp . Value ) ;
135
+ }
136
+ response . StatusCode = resp . HttpResponseCode ;
137
+
138
+ try
139
+ {
140
+ string json = Encoding . UTF8 . GetString ( resp . Data ) ;
141
+ response . Result = JsonConvert . DeserializeObject < TokenData > ( json ) ;
142
+ response . Response = json ;
143
+ }
144
+ catch ( Exception e )
145
+ {
146
+ Log . Error ( "Credentials.OnRequestIamTokenResponse()" , "Exception: {0}" , e . ToString ( ) ) ;
147
+ resp . Success = false ;
148
+ }
149
+
150
+ if ( ( ( RequestIamTokenRequest ) req ) . Callback != null )
151
+ ( ( RequestIamTokenRequest ) req ) . Callback ( response , resp . Error ) ;
152
+ }
153
+ #endregion
154
+ }
155
+
156
+ public class IamTokenOptions : JwtTokenOptions
157
+ {
158
+ private string iamApiKey ;
159
+ public string IamApiKey
160
+ {
161
+ get
162
+ {
163
+ return iamApiKey ;
164
+ }
165
+ set
166
+ {
167
+ if ( ! Utility . HasBadFirstOrLastCharacter ( value ) )
168
+ {
169
+ iamApiKey = value ;
170
+ }
171
+ else
172
+ {
173
+ throw new IBMException ( "The credentials shouldn't start or end with curly brackets or quotes. Be sure to remove any {} and \" characters surrounding your credentials" ) ;
174
+ }
175
+ }
176
+ }
177
+ public string IamAccessToken { get ; set ; }
178
+ public string IamUrl { get ; set ; }
179
+ public string IamClientId { get ; set ; }
180
+ public string IamClientSecret { get ; set ; }
181
+ }
182
+ }
0 commit comments