This repository has been archived by the owner on Jan 5, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
persona.go
111 lines (87 loc) · 2.23 KB
/
persona.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
package models
import (
"github.com/astaxie/beego"
"github.com/markbates/goth"
"github.com/markbates/goth/providers/github"
uuid "github.com/satori/go.uuid"
"menteslibres.net/gosexy/to"
)
var (
outwardUrl = beego.AppConfig.String("outwardloc")
githubKey = beego.AppConfig.String("auth::githubKey")
githubSecret = beego.AppConfig.String("auth::githubSecret")
)
func init() {
goth.UseProviders(github.New(githubKey, githubSecret, outwardUrl+"/auth/login/callback", "user"))
}
type GithubCheckController struct {
beego.Controller
}
func (this *GithubCheckController) Get() {
session := this.GetSession("github")
if session != nil {
emailLogin := to.String(session)
this.Ctx.WriteString(emailLogin)
} else {
this.Ctx.WriteString("")
}
}
type GithubLogoutController struct {
beego.Controller
}
func (this *GithubLogoutController) Get() {
this.DestroySession()
this.Ctx.WriteString("OK")
}
type GithubLoginController struct {
beego.Controller
}
func (this *GithubLoginController) Get() {
provider, err := goth.GetProvider("github")
if err != nil {
panic(err)
}
stateUUID := uuid.NewV4()
this.SetSession("oauth2State", stateUUID.String())
oauth2Sess, err := provider.BeginAuth(stateUUID.String())
if err != nil {
panic(err)
}
oauth2URL, err := oauth2Sess.GetAuthURL()
if err != nil {
panic(err)
}
this.SetSession("oauth2Sess", oauth2Sess.Marshal())
referrer := this.Ctx.Request.Referer()
if referrer == "" {
this.SetSession("login-referrer", outwardUrl)
} else {
this.SetSession("login-referrer", referrer)
}
this.Redirect(oauth2URL, 307)
}
type GithubLoginCallbackController struct {
beego.Controller
}
func (this *GithubLoginCallbackController) Get() {
provider, err := goth.GetProvider("github")
if err != nil {
panic(err)
}
oauth2Sess, err := provider.UnmarshalSession(to.String(this.GetSession("oauth2Sess")))
if err != nil {
panic(err)
}
_, err = oauth2Sess.Authorize(provider, this.Ctx.Request.URL.Query())
if err != nil {
panic(err)
}
oauth2User, err := provider.FetchUser(oauth2Sess)
if err != nil {
panic(err)
}
go FindUser(oauth2User.Email)
this.SetSession("github", oauth2User.Email)
referrer := to.String(this.GetSession("login-referrer"))
this.Redirect(referrer, 307)
}