-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
148 lines (112 loc) · 3.53 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
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
package main
import (
"encoding/json"
"flag"
"fmt"
"github.com/google/uuid"
"github.com/gorilla/mux"
"github.com/heavybr/oauth-client/pkg/client"
"github.com/heavybr/oauth-client/pkg/routes"
"github.com/joho/godotenv"
"golang.org/x/net/context"
"log"
"net/http"
"os"
"time"
)
func main() {
ctx := context.Background()
r := mux.NewRouter()
setupEnvironment()
var clientID, clientSecret, providerURL string
provider := flag.String("provider", "", "use oauth0 or google as provider")
flag.Parse()
if *provider == "oauth0" {
clientID = os.Getenv("OAUTH0_CLIENT_ID")
clientSecret = os.Getenv("OAUTH0_CLIENT_SECRET")
providerURL = os.Getenv("OAUTH0_PROVIDER_URL")
} else if *provider == "google" {
clientID = os.Getenv("GOOGLE_CLIENT_ID")
clientSecret = os.Getenv("GOOGLE_CLIENT_SECRET")
providerURL = os.Getenv("GOOGLE_PROVIDER_URL")
} else {
log.Fatal("choose between google or oauth0")
}
verifier, config, err := client.GetOpenIDClient(ctx, clientID, clientSecret, providerURL)
if err != nil {
log.Fatal(err.Error())
}
state := uuid.NewString()
r.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
http.Redirect(w, r, config.AuthCodeURL(state), http.StatusFound)
})
r.HandleFunc("/auth/callback", func(w http.ResponseWriter, r *http.Request) {
if r.URL.Query().Get("state") != state {
http.Error(w, "state did not match", http.StatusBadRequest)
return
}
oauth2Token, err := config.Exchange(ctx, r.URL.Query().Get("code"))
if err != nil {
http.Error(w, "Failed to exchange token: " + err.Error(), http.StatusInternalServerError)
return
}
rawIDToken, ok := oauth2Token.Extra("id_token").(string)
if !ok {
http.Error(w, "No id_token field in oauth2 token.", http.StatusInternalServerError)
return
}
resp := struct {
AccessToken string `json:"access_token"`
TokenType string `json:"token_type,omitempty"`
Expiry time.Time `json:"expiry,omitempty"`
IDToken string `json:"id_token,omitempty"`
}{oauth2Token.AccessToken, oauth2Token.TokenType, oauth2Token.Expiry, rawIDToken}
data, err := json.MarshalIndent(resp, "", " ")
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
_, _ = w.Write(data)
})
r.HandleFunc("/whoami", func (w http.ResponseWriter, r *http.Request) {
idToken := r.Header.Get("id_token")
accessToken := r.Header.Get("access_token")
if idToken == "" {
http.Error(w, "you must provide an id_token", http.StatusUnauthorized)
return
}
token, err := verifier.Verify(ctx, idToken)
if err != nil {
http.Error(w, "Failed to verify ID Token: " + err.Error(), http.StatusInternalServerError)
return
}
resp := struct {
IDTokenClaims *json.RawMessage // ID Token payload is just JSON.
}{new(json.RawMessage)}
if err := token.Claims(&resp.IDTokenClaims); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
fmt.Println(resp)
err = token.VerifyAccessToken(accessToken)
if err != nil {
http.Error(w, err.Error(), http.StatusUnauthorized)
return
}
data, err := json.MarshalIndent(resp, "", " ")
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
_, _ = w.Write(data)
}).Methods("POST")
r.HandleFunc("/auth/logout", routes.LogoutHandler).Methods("GET")
log.Printf("listening on http://%s/", "127.0.0.1:8000")
log.Fatal(http.ListenAndServe("127.0.0.1:8000", r))
}
func setupEnvironment() {
err := godotenv.Load(".env")
if err != nil {
log.Fatal("fail to read .env file")
}
}