/
oauth2.go
157 lines (135 loc) · 3.53 KB
/
oauth2.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
149
150
151
152
153
154
155
156
157
package oauth2
import (
"crypto/rand"
"encoding/base64"
"encoding/gob"
"encoding/json"
"fmt"
"github.com/gin-gonic/contrib/sessions"
"github.com/gin-gonic/gin"
"github.com/sirupsen/logrus"
"io/ioutil"
"net/http"
"golang.org/x/oauth2"
)
type Credentials struct {
ClientID string `json:"clientid"`
ClientSecret string `json:"secret"`
}
var (
conf *oauth2.Config
state string
store sessions.CookieStore
userRequestURL string
)
func randToken() string {
b := make([]byte, 32)
if _, err := rand.Read(b); err != nil {
logrus.Fatalf("[OAuth2] Failed to read rand: %v\n", err)
}
return base64.StdEncoding.EncodeToString(b)
}
func Setup(redirectURL, authURL, tokenURL, userURL, credFile string, scopes []string, secret []byte) {
userRequestURL = userURL
store = sessions.NewCookieStore(secret)
var c Credentials
file, err := ioutil.ReadFile(credFile)
if err != nil {
logrus.Fatalf("[OAuth2] File error: %v\n", err)
}
err = json.Unmarshal(file, &c)
if err != nil {
logrus.Fatalf("[OAuth2] Failed to unmarshal client credentials: %v\n", err)
}
conf = &oauth2.Config{
ClientID: c.ClientID,
ClientSecret: c.ClientSecret,
RedirectURL: redirectURL,
Scopes: scopes,
Endpoint: oauth2.Endpoint{
AuthURL: authURL,
TokenURL: tokenURL,
},
}
}
func Session(name string) gin.HandlerFunc {
return sessions.Sessions(name, store)
}
func loginRedirect(ctx *gin.Context) {
state = randToken()
session := sessions.Default(ctx)
session.Set("state", state)
session.Save()
ctx.Redirect(http.StatusTemporaryRedirect, GetLoginURL(state))
}
func GetLoginURL(state string) string {
return conf.AuthCodeURL(state)
}
type AuthUser struct {
Login string `json:"login"`
Name string `json:"name"`
Email string `json:"email"`
Company string `json:"company"`
URL string `json:"url"`
}
type userResponse struct {
ID int `json:"id"`
Name string `json:"name"`
Login string `json:"userid"`
}
func init() {
gob.Register(AuthUser{})
}
func Auth() gin.HandlerFunc {
return func(ctx *gin.Context) {
var (
ok bool
authUser AuthUser
user userResponse
)
// Handle the exchange code to initiate a transport.
session := sessions.Default(ctx)
mysession := session.Get("ginoauthgh")
if authUser, ok = mysession.(AuthUser); ok {
ctx.Set("user", authUser)
ctx.Next()
return
}
retrievedState := session.Get("state")
if retrievedState != ctx.Query("state") {
loginRedirect(ctx)
ctx.Abort()
return
}
// TODO: oauth2.NoContext -> context.Context from stdlib
tok, err := conf.Exchange(oauth2.NoContext, ctx.Query("code"))
if err != nil {
ctx.AbortWithError(http.StatusBadRequest, fmt.Errorf("Failed to do exchange: %v", err))
return
}
client := conf.Client(oauth2.NoContext, tok)
req, err := http.NewRequest("GET", userRequestURL, nil)
// Headers
req.Header.Add("Authorization", fmt.Sprintf("Bearer %s", tok.AccessToken))
req.Header.Add("Content-Type", "application/json; charset=utf-8")
// Fetch Request
resp, err := client.Do(req)
if err != nil {
ctx.AbortWithError(http.StatusBadRequest, fmt.Errorf("Failed to get user: %v", err))
return
}
respBody, _ := ioutil.ReadAll(resp.Body)
json.Unmarshal(respBody, &user)
// save userinfo, which could be used in Handlers
authUser = AuthUser{
Login: user.Login,
Name: user.Name,
}
ctx.Set("user", authUser)
// populate cookie
session.Set("ginoauthgh", authUser)
if err := session.Save(); err != nil {
logrus.Errorf("Failed to save session: %v", err)
}
}
}