1
+ /**
2
+ * @license
3
+ * Copyright 2020 Google LLC
4
+ *
5
+ * Licensed under the Apache License, Version 2.0 (the "License");
6
+ * you may not use this file except in compliance with the License.
7
+ * You may obtain a copy of the License at
8
+ *
9
+ * http://www.apache.org/licenses/LICENSE-2.0
10
+ *
11
+ * Unless required by applicable law or agreed to in writing, software
12
+ * distributed under the License is distributed on an "AS IS" BASIS,
13
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
+ * See the License for the specific language governing permissions and
15
+ * limitations under the License.
16
+ */
17
+
18
+ import { querystring } from '@firebase/util' ;
19
+
20
+ import { performFetchWithErrorHandling } from '../' ;
21
+ import { Auth } from '../../model/auth' ;
22
+
23
+ const ENDPOINT = 'https://securetoken.googleapis.com/v1/token' ;
24
+ const GRANT_TYPE = 'refresh_token' ;
25
+
26
+ export interface RequestStsTokenResponse {
27
+ access_token ?: string ;
28
+ expires_in ?: string ;
29
+ token_type ?: string ;
30
+ refresh_token ?: string ;
31
+ id_token ?: string ;
32
+ user_id ?: string ;
33
+ project_id ?: string ;
34
+ }
35
+
36
+ export async function requestStsToken ( auth : Auth , refreshToken : string ) : Promise < RequestStsTokenResponse > {
37
+ return performFetchWithErrorHandling < RequestStsTokenResponse > ( auth , { } , ( ) => {
38
+ const body = querystring ( {
39
+ 'grant_type' : GRANT_TYPE ,
40
+ 'refresh_token' : refreshToken ,
41
+ } ) . slice ( 1 ) ;
42
+
43
+ return fetch ( `${ ENDPOINT } ?key=${ auth . config . apiKey } ` , {
44
+ method : 'POST' ,
45
+ headers : {
46
+ 'X-Client-Version' : auth . config . sdkClientVersion ,
47
+ } ,
48
+ body,
49
+ } ) ;
50
+ } ) ;
51
+ }
0 commit comments