-
Notifications
You must be signed in to change notification settings - Fork 2
/
loginHandler.go
85 lines (77 loc) · 1.97 KB
/
loginHandler.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
package main
import (
"fmt"
"net/http"
oauth2 "github.com/Ulbora/go-oauth2-client"
)
// login handler
func handleLogout(res http.ResponseWriter, req *http.Request) {
//session, err := s.GetSession(req)
//if err != nil {
//http.Error(res, err.Error(), http.StatusInternalServerError)
//} else {
token = nil
cookie := &http.Cookie{
Name: "user-session",
Value: "",
Path: "/",
MaxAge: -1,
}
http.SetCookie(res, cookie)
cookie2 := &http.Cookie{
Name: "ulbora_oauth2_server",
Value: "",
Path: "/",
MaxAge: -1,
}
http.SetCookie(res, cookie2)
//session.Values["userLoggenIn"] = false
//session.Save(req, res)
http.Redirect(res, req, "/", http.StatusFound)
//}
}
func authorize(res http.ResponseWriter, req *http.Request) bool {
fmt.Println("in authorize")
fmt.Println(schemeDefault)
var a oauth2.AuthCodeAuthorize
a.ClientID = getAuthCodeClient()
a.OauthHost = getOauthRedirectHost()
a.RedirectURI = getRedirectURI(req, "/admin/token")
a.Scope = "write"
a.State = authCodeState
a.Res = res
a.Req = req
resp := a.AuthCodeAuthorizeUser()
if resp != true {
fmt.Println("Authorize Failed")
}
//fmt.Print("Resp: ")
//fmt.Println(resp)
return resp
}
func handleToken(res http.ResponseWriter, req *http.Request) {
code := req.URL.Query().Get("code")
state := req.URL.Query().Get("state")
if state == authCodeState {
var tn oauth2.AuthCodeToken
tn.OauthHost = getOauthHost()
tn.ClientID = getAuthCodeClient()
tn.Secret = getAuthCodeSecret()
tn.Code = code
tn.RedirectURI = getRedirectURI(req, "/admin/token")
resp := tn.AuthCodeToken()
if resp != nil && resp.AccessToken != "" {
//fmt.Println(resp.AccessToken)
token = resp
session, err := s.GetSession(req)
if err != nil {
http.Error(res, err.Error(), http.StatusInternalServerError)
} else {
session.Values["userLoggenIn"] = true
session.Save(req, res)
http.Redirect(res, req, "/admin/main", http.StatusFound)
// decode token and get user id
}
}
}
}