forked from tidbyt/pixlet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
example.star
70 lines (60 loc) · 1.91 KB
/
example.star
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
load("http.star", "http")
load("render.star", "render")
load("schema.star", "schema")
load("secret.star", "secret")
load("encoding/json.star", "json")
OAUTH2_CLIENT_SECRET = secret.decrypt("your-client-secret")
EXAMPLE_PARAMS = """
{"code": "your-code", "grant_type": "authorization_code", "client_id": "your-client-id", "redirect_uri": "https://appauth.tidbyt.com/your-app-id"}
"""
def main(config):
token = config.get("auth")
if token:
msg = "Authenticated"
else:
msg = "Unauthenticated"
return render.Root(
child = render.Marquee(
width = 64,
child = render.Text(msg),
),
)
def oauth_handler(params):
# deserialize oauth2 parameters, see example above.
params = json.decode(params)
# exchange parameters and client secret for an access token
res = http.post(
url = "https://github.com/login/oauth/access_token",
headers = {
"Accept": "application/json",
},
form_body = dict(
params,
client_secret = OAUTH2_CLIENT_SECRET,
),
form_encoding = "application/x-www-form-urlencoded",
)
if res.status_code != 200:
fail("token request failed with status code: %d - %s" %
(res.status_code, res.body()))
token_params = res.json()
access_token = token_params["access_token"]
return access_token
def get_schema():
return schema.Schema(
version = "1",
fields = [
schema.OAuth2(
id = "auth",
name = "GitHub",
desc = "Connect your GitHub account.",
icon = "github",
handler = oauth_handler,
client_id = "your-client-id",
authorization_endpoint = "https://github.com/login/oauth/authorize",
scopes = [
"read:user",
],
),
],
)