-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoauth.go
58 lines (45 loc) · 1.09 KB
/
oauth.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
package main
import (
"net/http"
"strings"
oa "google.golang.org/api/oauth2/v2"
)
const (
oauthURL = "https://oauth2.googleapis.com/tokeninfo?id_token="
)
var (
httpClient = &http.Client{}
)
func auth(r *http.Request) bool {
var token string
tokens, ok := r.Header["Authorization"]
if ok && len(tokens) >= 1 {
token = strings.TrimPrefix(tokens[0], "Bearer ")
}
if token == "" {
logger.Println("Token not set")
return false
}
oaSrv, err := oa.New(httpClient)
if err != nil {
logger.Printf("Error creating OAuth client: %v", err)
return false
}
info, err := oaSrv.Tokeninfo().IdToken(token).Do()
if err != nil {
logger.Printf("Error validating token: %v", err)
return false
}
logger.Printf("Token: %+v", info)
// TODO: Validate host portion of audience is equal to the current request host
// hosts := r.Header["Authorization"]
// if info.Audience != host {
// logger.Printf("Token for invalid client ID: %s", info.Audience)
// return false
// }
if !info.VerifiedEmail {
logger.Printf("Token for unverified email: %s", info.Audience)
return false
}
return true
}