-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
80 lines (65 loc) · 1.72 KB
/
main.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
package main
import (
"context"
"encoding/json"
"flag"
"fmt"
"github.com/golang-jwt/jwt/v5"
"os"
"tailscale.com/client/tailscale"
)
func main() {
aud := flag.String("aud", "", "")
flag.Parse()
if aud == nil || len(*aud) == 0 {
logFatal("400", "Missing -aud flag", fmt.Errorf("missing -aud flag"))
}
var tsclient tailscale.LocalClient
token, err := tsclient.IDToken(context.Background(), *aud)
if err != nil {
logFatal("401", "Caller not authorized.", err)
}
claims := jwt.MapClaims{}
parser := jwt.NewParser()
withClaims, _, err := parser.ParseUnverified(token.IDToken, claims)
if err != nil {
logFatal("500", "", err)
}
exp, err := withClaims.Claims.GetExpirationTime()
if err != nil {
logFatal("500", "", err)
}
c := &credentials{
Version: 1,
Success: true,
TokenType: "urn:ietf:params:oauth:token-type:id_token",
IDToken: token.IDToken,
ExpirationTime: exp,
}
marshal, _ := json.MarshalIndent(c, "", " ")
fmt.Println(string(marshal))
}
func logFatal(code string, msg string, err error) {
c := &credentials{
Version: 1,
Success: false,
Code: code,
Message: msg,
}
if msg == "" {
c.Message = err.Error()
}
marshal, _ := json.MarshalIndent(c, "", " ")
fmt.Println(string(marshal))
_, _ = fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
type credentials struct {
Version int `json:"version,omitempty"`
Success bool `json:"success,omitempty"`
TokenType string `json:"token_type,omitempty"`
IDToken string `json:"id_token,omitempty"`
ExpirationTime *jwt.NumericDate `json:"expiration_time,omitempty"`
Code string `json:"code,omitempty"`
Message string `json:"message,omitempty"`
}