-
Notifications
You must be signed in to change notification settings - Fork 0
/
oauth.go
49 lines (45 loc) · 1.34 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
package main
import (
"fmt"
"net/http"
"time"
"github.com/CoPhi/cophi-auth-service/auth"
"github.com/CoPhi/cophi-auth-service/refreshtoken"
"github.com/gorilla/sessions"
"github.com/markbates/goth"
"github.com/markbates/goth/gothic"
"github.com/markbates/goth/providers/google"
)
func setupProviders(rootURL, googleClientID, googleSecret string) {
gothic.Store = sessions.NewCookieStore([]byte("<your secret here>")) // TODO: this is unused, it just avoid warning messages
goth.UseProviders(
google.New(
googleClientID,
googleSecret,
rootURL+"/callback/oauth",
"email", // TODO: check, maybe userinf.email is enough
"https://www.googleapis.com/auth/userinfo.email",
"https://www.googleapis.com/auth/userinfo.profile",
),
)
}
func oauthCallback(privKey, domain string, rts refreshtoken.Store, jwtExpiration time.Duration) func(w http.ResponseWriter, r *http.Request) {
return func(w http.ResponseWriter, r *http.Request) {
oauthUser, err := gothic.CompleteUserAuth(w, r)
if err != nil {
fmt.Fprintln(w, err)
return
}
user := auth.AuthUser{
Email: oauthUser.Email,
Name: oauthUser.FirstName,
LastName: oauthUser.LastName,
}
referer, err := r.Cookie("referer")
if err != nil {
fmt.Fprintln(w, err)
return
}
auth.AuthCallback(referer.Value, rts, &user, privKey, domain, jwtExpiration)(w, r)
}
}