-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoauth_callback.go
110 lines (75 loc) · 2.59 KB
/
oauth_callback.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
package handler
import (
"log"
"net/http"
"github.com/zmzlois/LinkGoGo/pkg/auth"
"github.com/zmzlois/LinkGoGo/pkg/model"
"github.com/zmzlois/LinkGoGo/pkg/service"
)
type AuthHandler struct {
AuthService service.AuthService
}
func NewAuthHandler(service service.AuthService) *AuthHandler {
return &AuthHandler{AuthService: service}
}
func (h *AuthHandler) OAuthHandler(w http.ResponseWriter, r *http.Request) {
h.AuthService.Redirect(w, r)
}
func (h *AuthHandler) OAuthCallbackHandler(w http.ResponseWriter, r *http.Request) {
// var dc auth.Client
// var userService service.UserService
var dsc = auth.DiscordInit()
var userService = &service.UserService{}
// Get the code from the redirect parameters (&code=...)
var codeFromURLParamaters = r.URL.Query()["code"][0]
// get access token map
data, err1 := dsc.GetAccessTokenMap(codeFromURLParamaters)
accessToken := data["token_type"].(string) + " " + data["access_token"].(string)
if err1 != nil {
log.Printf("OAuthCallbackHandler.Fialed to get access token: %s", err1)
}
user, err := dsc.GetUserData(accessToken)
if err != nil {
log.Printf("OAuthCallbackHandler.Failed to get user data: %s", err)
}
// processing the data coming in
userData, err := model.ParsingUserInput(user)
if err != nil {
log.Printf("[OAuth Redirect.Redirect.ParsingUserInput]: %s", err)
}
// check if this user exists?
tokenPayload, err := model.ParsingTokenInput(data)
if err != nil {
log.Printf("[OAuth Redirect.Redirect]: %s", err)
}
// creating signed jwt token
tokenString, err := dsc.CreateToken(userData, tokenPayload, h.AuthService.State)
// if user exists, redirect to edit page, update their session
if err != nil {
log.Printf("[OAuth Redirect.Redirect]: %s", err)
}
userExist, err := h.AuthService.Login(userData.Id, tokenString)
if err != nil {
log.Printf("[OAuth Redirect.Login]: %s", err)
}
if userExist != nil {
log.Printf("[OAuth Redirect.Redirect]: User %s exists", userExist.Username)
dsc.SetCookie(tokenString, w)
http.Redirect(w, r, "/edit", http.StatusFound)
return
}
// if we can't find this user we creat a new user
User, Session, err := userService.CreateUser(userData, tokenPayload, tokenString)
if err != nil {
log.Printf("[OAuth Redirect.Redirect]: %s", err)
}
if User == nil || Session == nil {
log.Printf("[OAuth Redirect.Redirect]: User or Session is nil")
}
if User != nil && Session != nil {
log.Printf("[OAuth Redirect.Redirect]: User %s and Session created", User.Username)
}
// set token in cookie
dsc.SetCookie(tokenString, w)
http.Redirect(w, r, "/edit", http.StatusFound)
}