-
Notifications
You must be signed in to change notification settings - Fork 1
/
auth.gleam
170 lines (160 loc) · 4.68 KB
/
auth.gleam
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
import gleam/dynamic.{type Dynamic}
import gleam/result
import gleam/uri.{type Uri}
import gleam/http.{Get, Post}
import glitch/api/api
import glitch/api/api_response
import glitch/api/api_request
import glitch/error.{type TwitchError, AuthError, InvalidGetTokenRequest}
import glitch/types/access_token.{type AccessToken}
import glitch/types/grant.{
type GrantType, AuthorizationCode, ClientCredentials, RefreshToken,
}
import glitch/types/scope.{type Scope}
pub opaque type GetTokenRequest {
AuthorizationCodeGrant(
client_id: String,
client_secret: String,
code: String,
grant_type: GrantType,
redirect_uri: Uri,
)
ClientCredentialsGrant(
client_id: String,
client_secret: String,
grant_type: GrantType,
)
RefreshTokenGrant(
client_id: String,
client_secret: String,
grant_type: GrantType,
refresh_token: String,
)
}
pub fn new_authorization_code_grant_request(
client_id client_id: String,
client_secret client_secret: String,
code code: String,
redirect_uri redirect_uri: Uri,
) -> GetTokenRequest {
AuthorizationCodeGrant(
client_id,
client_secret,
code,
AuthorizationCode,
redirect_uri,
)
}
pub fn new_client_credentials_grant_request(
client_id client_id: String,
client_secret client_secret: String,
) -> GetTokenRequest {
ClientCredentialsGrant(client_id, client_secret, ClientCredentials)
}
pub fn new_refresh_token_grant_request(
client_id client_id: String,
client_secret client_secret: String,
refresh_token refresh_token: String,
) -> GetTokenRequest {
RefreshTokenGrant(client_id, client_secret, RefreshToken, refresh_token)
}
fn get_token_request_to_form_data(get_token_request: GetTokenRequest) -> String {
case get_token_request {
AuthorizationCodeGrant(
client_id,
client_secret,
code,
grant_type,
redirect_uri,
) -> [
#("client_id", client_id),
#("client_secret", client_secret),
#("code", code),
#("grant_type", grant.to_string(grant_type)),
#("redirect_uri", uri.to_string(redirect_uri)),
]
ClientCredentialsGrant(client_id, client_secret, grant_type) -> [
#("client_id", client_id),
#("client_secret", client_secret),
#("grant_type", grant.to_string(grant_type)),
]
RefreshTokenGrant(client_id, client_secret, grant_type, refresh_token) -> [
#("client_id", client_id),
#("client_secret", client_secret),
#("grant_type", grant.to_string(grant_type)),
#("refresh_token", uri.percent_encode(refresh_token)),
]
}
|> uri.query_to_string
}
pub fn get_token(
get_token_request: GetTokenRequest,
) -> Result(AccessToken, TwitchError) {
case get_token_request {
RefreshTokenGrant(_, _, _, _) -> Error(AuthError(InvalidGetTokenRequest))
_ -> {
api_request.new_auth_request()
|> api_request.set_body(get_token_request_to_form_data(get_token_request))
|> api_request.set_path("oauth2/token")
|> api_request.set_header(#(
"content-type",
"application/x-www-form-urlencoded",
))
|> api_request.set_method(Post)
|> api.send
|> result.try(api_response.get_data(_, access_token.decoder()))
}
}
}
pub fn refresh_token(
get_token_request: GetTokenRequest,
) -> Result(AccessToken, TwitchError) {
case get_token_request {
RefreshTokenGrant(_, _, _, _) -> {
api_request.new_auth_request()
|> api_request.set_body(get_token_request_to_form_data(get_token_request))
|> api_request.set_path("oauth2/token")
|> api_request.set_header(#(
"content-type",
"application/x-www-form-urlencoded",
))
|> api_request.set_method(Post)
|> api.send
|> result.try(api_response.get_data(_, access_token.decoder()))
}
_ -> Error(AuthError(InvalidGetTokenRequest))
}
}
pub type ValidateTokenResponse {
ValidateTokenResponse(
client_id: String,
login: String,
scopes: List(Scope),
user_id: String,
expires_in: Int,
)
}
fn validate_token_response_decoder() {
fn(data: Dynamic) {
data
|> dynamic.decode5(
ValidateTokenResponse,
dynamic.field("client_id", dynamic.string),
dynamic.field("login", dynamic.string),
dynamic.field("scopes", dynamic.list(scope.decoder())),
dynamic.field("user_id", dynamic.string),
dynamic.field("expires_in", dynamic.int),
)
}
}
pub fn validate_token(access_token: AccessToken) {
api_request.new_auth_request()
|> api_request.set_path("oauth2/validate")
|> api_request.set_header(#(
"Authorization",
"OAuth " <> access_token.token(access_token),
))
|> api_request.set_method(Get)
|> api.send
|> result.try(api_response.get_data(_, validate_token_response_decoder()))
}