-
Notifications
You must be signed in to change notification settings - Fork 1
/
deviceflow.go
94 lines (89 loc) · 2.63 KB
/
deviceflow.go
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
package sshca
import (
"encoding/json"
"errors"
"io"
"net/http"
"net/url"
"time"
)
func deviceflowHandler(w http.ResponseWriter, r *http.Request, ca CaConfig) (err error) {
token := claims.set("", certInfo{ca: ca.Id})
resp, err := device_authorization_request(ca.ClientID, ca.Op.Device_authorization)
if err != nil {
return
}
tmpl.ExecuteTemplate(w, ca.HTMLTemplate, map[string]any{"ca": ca, "state": token, "sshport": Config.SshPort, "verification_uri": resp["verification_uri_complete"].(string), "op": ca.Id, "rp": Config.RelayingParty, "ri": "//" + r.Host + "/" + ca.Id + "/ri"})
go func(token string) {
tokenResponse, _ := token_request(ca.ClientID, ca.Op.Token, resp["device_code"].(string))
if tokenResponse != nil {
userInfo, err := getUserinfo(tokenResponse["access_token"].(string), ca.Op.Userinfo)
if err != nil {
return
}
val, ok := userInfo["sub"].(string)
if ok {
ci := certInfo{ca: ca.Id, principal: val, username: usernameFromPrincipal(val, ca), eol: time.Now().Add(rendevouzTTL)}
claims.set(token, ci)
}
}
}(token)
return
}
func device_authorization_request(clientID, device_authorization string) (res map[string]any, err error) {
v := url.Values{}
v.Set("client_id", clientID)
v.Set("scope", "openid email profile eduperson_entitlement")
resp, err := client.PostForm(device_authorization, v)
if err != nil {
return
}
responsebody, err := io.ReadAll(resp.Body)
if err != nil {
return
}
res = map[string]any{}
json.Unmarshal(responsebody, &res)
return
}
func token_request(clientID, token, device_code string) (res map[string]any, err error) {
v := url.Values{}
v.Set("grant_type", "urn:ietf:params:oauth:grant-type:device_code")
v.Set("device_code", device_code)
v.Set("client_id", clientID)
tries := 10
timeout := 2 * time.Second
for tries > 0 {
tries--
resp, err := client.PostForm(token, v)
if err != nil {
return nil, err
} else if 200 <= resp.StatusCode && resp.StatusCode < 300 {
responsebody, _ := io.ReadAll(resp.Body)
res = map[string]any{}
json.Unmarshal(responsebody, &res)
return res, nil
} else {
time.Sleep(timeout)
continue
}
}
return nil, errors.New("")
}
func getUserinfo(token, endpoint string) (res map[string]any, err error) {
request, _ := http.NewRequest("POST", endpoint, nil)
request.Header.Add("Content-Type", "application/x-www-form-urlencoded")
request.Header.Add("Authorization", "Bearer "+token)
resp, err := client.Do(request)
if err != nil {
return
}
defer resp.Body.Close()
responsebody, err := io.ReadAll(resp.Body)
if err != nil {
return
}
res = map[string]any{}
json.Unmarshal(responsebody, &res)
return
}