-
Notifications
You must be signed in to change notification settings - Fork 80
/
Copy pathauth.go
129 lines (107 loc) · 2.71 KB
/
auth.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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
package user
import (
"fmt"
"gitlab.com/leanlabsio/kanban/models"
"gitlab.com/leanlabsio/kanban/modules/auth"
"gitlab.com/leanlabsio/kanban/modules/middleware"
"gopkg.in/macaron.v1"
"log"
"net/http"
)
// OauthUrl redirects to url for authorisation
func OauthUrl(ctx *middleware.Context) {
ctx.Redirect(models.AuthCodeURL(ctx.Query("provider")))
}
// OauthLogin logins with gitlab and get access token
func OauthLogin(ctx *middleware.Context, form auth.Oauth2) {
tok, err := models.Exchange(form.Provider, form.Code)
if err != nil {
log.Printf("%s", err.Error())
ctx.JSON(http.StatusBadRequest, models.ResponseError{
Success: false,
Message: err.Error(),
})
return
}
user, err := models.UserOauthSignIn(form.Provider, tok)
if err != nil {
log.Printf("%s", err.Error())
ctx.JSON(http.StatusBadRequest, models.ResponseError{
Success: false,
Message: err.Error(),
})
return
}
user, err = models.LoadByToken(user, ctx.Provider)
if err != nil {
log.Printf("%s", err.Error())
ctx.JSON(http.StatusBadRequest, models.ResponseError{
Success: false,
Message: err.Error(),
})
return
}
user.Username = fmt.Sprintf("%s_%s", user.Username, ctx.Provider)
_, err = models.UpdateUser(user)
// todo add validation by oauth provider
if err != nil {
user, err = models.CreateUser(user)
}
if err != nil {
log.Printf("%s", err.Error())
ctx.JSON(http.StatusInternalServerError, models.ResponseError{
Success: false,
Message: err.Error(),
})
return
}
tokens, err := user.SignedString()
if err != nil {
log.Printf("%s", err.Error())
ctx.JSON(http.StatusBadRequest, models.ResponseError{
Success: false,
Message: err.Error(),
})
return
}
ctx.JSON(http.StatusOK, auth.ResponseAuth{
Success: true,
Token: tokens,
})
}
// SignIn registers with user data
func SignIn(ctx *macaron.Context, form auth.SignIn) {
u, err := models.UserSignIn(form.Uname, form.Pass)
if err != nil {
ctx.JSON(http.StatusBadRequest, models.ResponseError{
Success: false,
Message: err.Error(),
})
return
}
tokens, _ := u.SignedString()
ctx.JSON(http.StatusOK, auth.ResponseAuth{
Success: true,
Token: tokens,
})
}
// SignUp sing ups with data
func SignUp(ctx *middleware.Context, form auth.SignUp) {
u, err := models.UserSignUp(form.Uname, form.Email, form.Pass, form.Token, ctx.Provider)
if err != nil {
ctx.JSON(http.StatusBadRequest, models.ResponseError{
Success: false,
Message: err.Error(),
})
return
}
tokens, _ := u.SignedString()
ctx.JSON(http.StatusOK, auth.ResponseAuth{
Success: true,
Token: tokens,
})
}
// OauthHandler handles request from other services
func OauthHandler(ctx *middleware.Context) {
ctx.HTML(200, "templates/oauth")
}