-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhandler.go
158 lines (128 loc) · 3.58 KB
/
handler.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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
package main
import (
"net/http"
"github.com/labstack/echo"
uuid "github.com/satori/go.uuid"
)
type signinInput struct {
AppUUID string `json:"appId"`
Provider string `json:"provider"`
Payload string `json:"payload"`
}
type signinOutput struct {
AccessToken string `json:"accessToken"`
RefreshToken string `json:"refreshToken"`
ExpireAt int64 `json:"expireAt"`
}
func signin(c echo.Context) error {
var input signinInput
c.Bind(&input)
switch input.Provider {
case "google":
u, err := getGoogleUser(input.Payload)
if err != nil {
return errorAPI(c, http.StatusUnauthorized, "unauthorized")
}
var account UserAccount
db.Where(&UserAccount{Provider: "google", ProviderIdentity: u.Email}).First(&account)
if account.ID == 0 {
return errorAPI(c, http.StatusUnauthorized, "no_such_account")
}
// TODO - check if identity with email already exists
identity := account.GetIdentity()
var app Application
db.Where(&Application{UUID: input.AppUUID}).First(&app)
var allowed int
db.Model(&UserAllowedApplication{}).
Where(&UserAllowedApplication{ApplicationID: app.ID, IdentityID: identity.ID}).
Count(&allowed)
if allowed == 0 {
return errorAPI(c, http.StatusUnauthorized, "authorization_required")
}
token := identity.NewToken(app.Scopes)
output := signinOutput{
AccessToken: token.AccessToken,
RefreshToken: token.RefreshToken,
ExpireAt: token.AccessTokenExpireAt.Unix() * 1000,
}
return c.JSON(http.StatusOK, output)
default:
return errorAPI(c, http.StatusBadRequest, "no_such_provider")
}
}
type registerInput struct {
Provider string `json:"provider"`
Payload string `json:"payload"`
Username string `json:"username"`
}
func register(c echo.Context) error {
var input registerInput
c.Bind(&input)
switch input.Provider {
case "google":
u, err := getGoogleUser(input.Payload)
if err != nil {
return errorAPI(c, http.StatusUnauthorized, "unauthorized")
}
identity := UserIdentity{
UUID: uuid.NewV4().String(),
Username: input.Username,
Email: u.Email,
}
db.Save(&identity)
account := UserAccount{
IdentityID: identity.ID,
Provider: "google",
ProviderIdentity: u.Email,
}
db.Save(&account)
return c.String(http.StatusCreated, "")
default:
return errorAPI(c, http.StatusBadRequest, "no_such_provider")
}
}
type authorizeInput struct {
AppUUID string `json:"appId"`
Provider string `json:"provider"`
Payload string `json:"payload"`
Username string `json:"username"`
}
func authorize(c echo.Context) error {
var input authorizeInput
c.Bind(&input)
switch input.Provider {
case "google":
u, err := getGoogleUser(input.Payload)
if err != nil {
return errorAPI(c, http.StatusUnauthorized, "unauthorized")
}
var account UserAccount
db.Where(&UserAccount{Provider: "google", ProviderIdentity: u.Email}).First(&account)
if account.ID == 0 {
return errorAPI(c, http.StatusUnauthorized, "no_such_account")
}
var app Application
db.Where(&Application{UUID: input.AppUUID}).First(&app)
rel := UserAllowedApplication{
ApplicationID: app.ID,
IdentityID: account.IdentityID,
}
db.Save(&rel)
return c.String(http.StatusCreated, "")
default:
return errorAPI(c, http.StatusBadRequest, "no_such_provider")
}
}
type getMeOutput struct {
UUID string `json:"uuid"`
Email string `json:"email"`
Username string `json:"username"`
}
func getMe(c echo.Context) error {
ctx := c.(AuthedContext)
return c.JSON(http.StatusOK, getMeOutput{
UUID: ctx.identity.UUID,
Email: ctx.identity.Email,
Username: ctx.identity.Username,
})
}