15
15
* limitations under the License.
16
16
*/
17
17
18
+ /* eslint-disable camelcase */
19
+
18
20
import { querystring } from '@firebase/util' ;
19
21
20
- import { performFetchWithErrorHandling } from '../' ;
22
+ import { HttpMethod , performFetchWithErrorHandling } from '../' ;
21
23
import { Auth } from '../../model/auth' ;
22
24
23
- export const _ENDPOINT = 'https://securetoken.googleapis.com/ v1/token' ;
25
+ export const _ENDPOINT = 'v1/token' ;
24
26
const GRANT_TYPE = 'refresh_token' ;
25
27
26
- enum ServerField {
27
- ACCESS_TOKEN = 'access_token' ,
28
- EXPIRES_IN = 'expires_in' ,
29
- REFRESH_TOKEN = 'refresh_token'
28
+ /** The server responses with snake_case; we convert to camelCase */
29
+ interface RequestStsTokenServerResponse {
30
+ access_token ?: string ;
31
+ expires_in ?: string ;
32
+ refresh_token ?: string ;
30
33
}
31
34
32
35
export interface RequestStsTokenResponse {
@@ -39,18 +42,18 @@ export async function requestStsToken(
39
42
auth : Auth ,
40
43
refreshToken : string
41
44
) : Promise < RequestStsTokenResponse > {
42
- const response = await performFetchWithErrorHandling < {
43
- [ key : string ] : string ;
44
- } > ( auth , { } , ( ) => {
45
+ const response = await performFetchWithErrorHandling < RequestStsTokenServerResponse > ( auth , { } , ( ) => {
45
46
const body = querystring ( {
46
47
'grant_type' : GRANT_TYPE ,
47
48
'refresh_token' : refreshToken
48
49
} ) . slice ( 1 ) ;
50
+ const { apiScheme, tokenApiHost, apiKey, sdkClientVersion} = auth . config ;
51
+ const url = `${ apiScheme } ://${ tokenApiHost } /${ _ENDPOINT } ` ;
49
52
50
- return fetch ( `${ _ENDPOINT } ?key=${ auth . config . apiKey } ` , {
51
- method : ' POST' ,
53
+ return fetch ( `${ url } ?key=${ apiKey } ` , {
54
+ method : HttpMethod . POST ,
52
55
headers : {
53
- 'X-Client-Version' : auth . config . sdkClientVersion ,
56
+ 'X-Client-Version' : sdkClientVersion ,
54
57
'Content-Type' : 'application/x-www-form-urlencoded'
55
58
} ,
56
59
body
@@ -59,8 +62,8 @@ export async function requestStsToken(
59
62
60
63
// The response comes back in snake_case. Convert to camel:
61
64
return {
62
- accessToken : response [ ServerField . ACCESS_TOKEN ] ,
63
- expiresIn : response [ ServerField . EXPIRES_IN ] ,
64
- refreshToken : response [ ServerField . REFRESH_TOKEN ]
65
+ accessToken : response . access_token ,
66
+ expiresIn : response . expires_in ,
67
+ refreshToken : response . refresh_token
65
68
} ;
66
69
}
0 commit comments