-
Notifications
You must be signed in to change notification settings - Fork 199
Expand file tree
/
Copy pathgetOAuth2Token.ts
More file actions
66 lines (60 loc) · 1.91 KB
/
getOAuth2Token.ts
File metadata and controls
66 lines (60 loc) · 1.91 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
import { POST, request } from '../../request'
import type { ResponsePromise } from '../../request'
import type { HostConfig } from '../../types'
/** Our server currently expects us to supply this hard-coded client_id. */
export const OAUTH2_CLIENT_ID = 'opentrons_app' as const
/**
* An OAuth 2 "Resource owner password credentials" request,
* to exchange a username+password for an access token.
*
* https://datatracker.ietf.org/doc/html/rfc6749#section-4.3
*/
export interface ROPCRequest {
grant_type: 'password'
username: string
password: string
// RFC 6749 seems to say client_id shouldn't be necessary here for public
// clients like the Opentrons App, but our server currently requires it anyway.
client_id: typeof OAUTH2_CLIENT_ID
}
/**
* A refresh request, to get a new access token before the old one expires.
*
* https://datatracker.ietf.org/doc/html/rfc6749#section-6
*/
export interface RefreshRequest {
grant_type: 'refresh_token'
refresh_token: string
// RFC 6749 seems to say client_id shouldn't be necessary here for public
// clients like the Opentrons App, but our server currently requires it anyway.
client_id: typeof OAUTH2_CLIENT_ID
}
export interface OAuth2TokenResponse {
/**
* In practice, token_type will always be "Bearer" for us,
* but calling code should validate it and refuse token_types
* that it doesn't understand.
*/
token_type: string
access_token: string
refresh_token?: string
expires_in?: number /** In seconds. */
scope?: string
}
/**
* Obtain an OAuth 2 access token.
*
* https://datatracker.ietf.org/doc/html/rfc6749#section-3.2
*/
export function getOAuth2Token(
config: HostConfig,
body: ROPCRequest | RefreshRequest
): ResponsePromise<OAuth2TokenResponse> {
const encodedBody = new URLSearchParams({ ...body })
return request<OAuth2TokenResponse, URLSearchParams>(
POST,
'/auth/oauth2/token',
encodedBody,
config
)
}