-
Notifications
You must be signed in to change notification settings - Fork 1
/
access_token.gleam
160 lines (144 loc) · 4.01 KB
/
access_token.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
import gleam/dynamic.{type Decoder, type Dynamic}
import gleam/option.{type Option}
import gleam/result
import gleam/erlang
import glitch/error.{type TwitchError, AuthError, InvalidAccessToken}
import glitch/types/scope.{type Scope}
pub opaque type AccessToken {
AppAccessToken(
expires_in: Int,
obtained_at: Int,
token: String,
last_validated_at: Int,
)
UserAccessToken(
expires_in: Int,
obtained_at: Int,
refresh_token: String,
scopes: List(Scope),
token: String,
last_validated_at: Int,
)
}
type RawAccessToken {
RawAppAccessToken(access_token: String, expires_in: Int)
RawUserAccessToken(
access_token: String,
expires_in: Int,
refresh_token: String,
scope: List(Scope),
)
}
pub fn decoder() -> Decoder(AccessToken) {
fn(data: Dynamic) {
data
|> dynamic.decode4(
RawUserAccessToken,
dynamic.field("access_token", dynamic.string),
dynamic.field("expires_in", dynamic.int),
dynamic.field("refresh_token", dynamic.string),
dynamic.field("scope", dynamic.list(scope.decoder())),
)
|> result.map(from_raw_access_token)
}
}
fn from_raw_access_token(raw_access_token: RawAccessToken) -> AccessToken {
let now = erlang.system_time(erlang.Second)
case raw_access_token {
RawAppAccessToken(access_token, expires_in) ->
AppAccessToken(
expires_in: expires_in,
obtained_at: now,
token: access_token,
last_validated_at: now,
)
RawUserAccessToken(access_token, expires_in, refresh_token, scope) ->
UserAccessToken(
expires_in: expires_in,
obtained_at: now,
refresh_token: refresh_token,
scopes: scope,
token: access_token,
last_validated_at: now,
)
}
}
pub fn new_user_access_token(
expires_in: Int,
obtained_at: Int,
refresh_token: String,
scopes: List(Scope),
token: String,
last_validated_at: Option(Int),
) -> AccessToken {
UserAccessToken(
expires_in,
obtained_at,
refresh_token,
scopes,
token,
option.unwrap(last_validated_at, 0),
)
}
pub fn token(access_token: AccessToken) -> String {
case access_token {
UserAccessToken(_, _, _, _, token, _) -> token
AppAccessToken(_, _, token, _) -> token
}
}
pub fn refresh_token(access_token: AccessToken) -> Result(String, TwitchError) {
case access_token {
UserAccessToken(_, _, refresh_token, _, _, _) -> Ok(refresh_token)
_ -> Error(AuthError(InvalidAccessToken))
}
}
pub fn scopes(access_token: AccessToken) -> Result(List(Scope), TwitchError) {
case access_token {
UserAccessToken(_, _, _, scopes, _, _) -> Ok(scopes)
_ -> Error(AuthError(InvalidAccessToken))
}
}
pub fn is_expired(access_token: AccessToken) -> Bool {
let #(expires_in, obtained_at) = case access_token {
UserAccessToken(expires_in, obtained_at, _, _, _, _) -> #(
expires_in,
obtained_at,
)
AppAccessToken(expires_in, obtained_at, _, _) -> #(expires_in, obtained_at)
}
let now = erlang.system_time(erlang.Second)
let expire_time = obtained_at + expires_in
now > expire_time
}
pub fn needs_validated(access_token: AccessToken) -> Bool {
let last_validated_at = case access_token {
UserAccessToken(_, _, _, _, _, last_validated_at) -> last_validated_at
AppAccessToken(_, _, _, last_validated_at) -> last_validated_at
}
let now = erlang.system_time(erlang.Second)
let one_hour_seconds = 3600
let next_validation_time = last_validated_at + one_hour_seconds
now >= next_validation_time
}
pub fn set_expires_in(access_token: AccessToken, expires_in: Int) -> AccessToken {
case access_token {
UserAccessToken(
_,
obtained_at,
refresh_token,
scopes,
token,
last_validated_at,
) ->
UserAccessToken(
expires_in,
obtained_at,
refresh_token,
scopes,
token,
last_validated_at,
)
AppAccessToken(_, obtained_at, token, last_validated_at) ->
AppAccessToken(expires_in, obtained_at, token, last_validated_at)
}
}