-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathorg-email.go
101 lines (86 loc) · 2.36 KB
/
org-email.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
package controllers
import (
"fmt"
"strings"
"github.com/opensourceways/app-cla-server/email"
"github.com/opensourceways/app-cla-server/models"
)
const authURLState = "state-token-cla"
type EmailController struct {
baseController
}
func (this *EmailController) Prepare() {
if strings.HasSuffix(this.routerPattern(), "authcodeurl/:platform") {
this.apiPrepare(PermissionOwnerOfOrg)
}
}
// @Title Auth
// @Description authorized by org email
// @router /auth/:platform [get]
func (this *EmailController) Auth() {
rs := func(errCode string, reason error) {
this.setCookies(map[string]string{"error_code": errCode, "error_msg": reason.Error()}, false)
this.redirect(email.EmailAgent.WebRedirectDir(false))
}
if err := this.GetString("error"); err != "" {
rs(errAuthFailed, fmt.Errorf("%s, %s", err, this.GetString("error_description")))
return
}
platform := this.GetString(":platform")
emailClient, err := email.EmailAgent.GetEmailClient(platform)
if err != nil {
rs(errUnsupportedEmailPlatform, err)
return
}
if this.GetString("state") != authURLState {
rs(errSystemError, fmt.Errorf("unkown state"))
return
}
token, err := emailClient.GetToken(this.GetString("code"), this.GetString("scope"))
if err != nil {
rs(errSystemError, err)
return
}
emailAddr, err := emailClient.GetAuthorizedEmail(token)
if err != nil {
rs(errSystemError, err)
return
}
if token.RefreshToken == "" {
v, err := models.HasOrgEmail(emailAddr)
if err != nil {
rs(errSystemError, err)
return
}
if !v {
rs(errNoRefreshToken, fmt.Errorf("no refresh token"))
return
}
} else {
opt := models.OrgEmail{
Token: token,
Email: emailAddr,
Platform: platform,
}
if err := opt.Create(); err != nil {
rs(parseModelError(err).errCode, err)
return
}
}
this.setCookies(map[string]string{"email": emailAddr}, false)
this.redirect(email.EmailAgent.WebRedirectDir(true))
}
// @Title Get
// @Description get auth code url
// @Param platform path string true "The email platform"
// @router /authcodeurl/:platform [get]
func (this *EmailController) Get() {
e, err := email.EmailAgent.GetEmailClient(this.GetString(":platform"))
if err != nil {
this.sendFailedResponse(400, errUnknownEmailPlatform, err, "get auth code url of email")
return
}
this.sendSuccessResp(map[string]string{
"url": e.GetOauth2CodeURL(authURLState),
})
}