-
Notifications
You must be signed in to change notification settings - Fork 5
/
passwordless.go
189 lines (166 loc) · 5.48 KB
/
passwordless.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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
package controller
import (
"encoding/base64"
"encoding/json"
"fmt"
"net/http"
"strings"
"github.com/maximthomas/gortas/pkg/auth/constants"
"github.com/maximthomas/gortas/pkg/auth/state"
"github.com/maximthomas/gortas/pkg/log"
"github.com/maximthomas/gortas/pkg/middleware"
"github.com/maximthomas/gortas/pkg/session"
"github.com/maximthomas/gortas/pkg/user"
"github.com/gin-gonic/gin"
"github.com/google/uuid"
"github.com/maximthomas/gortas/pkg/config"
"github.com/sirupsen/logrus"
"github.com/skip2/go-qrcode"
)
const qrSize = 256
// TODO v2 refactor passwordless architecture
type PasswordlessServicesController struct {
logger logrus.FieldLogger
conf config.Config
}
func NewPasswordlessServicesController(c *config.Config) *PasswordlessServicesController {
logger := log.WithField("module", "PasswordlessServicesController")
return &PasswordlessServicesController{logger, *c}
}
type QRProps struct {
Secret string `json:"secret"`
}
func (pc *PasswordlessServicesController) RegisterGenerateQR(c *gin.Context) {
si, ok := c.Get("session")
if !ok {
c.AbortWithStatusJSON(http.StatusUnauthorized, gin.H{"error": "Not authenticated"})
return
}
s := si.(session.Session)
uid := s.GetUserID()
us := user.GetUserService()
_, ok = us.GetUser(uid)
if !ok {
c.AbortWithStatusJSON(http.StatusUnauthorized, gin.H{"error": "No user found in the repository"})
return
}
requestURI := middleware.GetRequestURI(c)
imageData := fmt.Sprintf("%s?sid=%s&action=register", requestURI, s.ID)
png, err := qrcode.Encode(imageData, qrcode.Medium, qrSize)
if err != nil {
pc.logger.Error(err)
c.AbortWithStatusJSON(http.StatusInternalServerError, gin.H{"error": "error generate QR code"})
return
}
image := "data:image/png;base64," + base64.StdEncoding.EncodeToString(png)
c.JSON(http.StatusOK, gin.H{"qr": image})
}
func (pc *PasswordlessServicesController) RegisterConfirmQR(c *gin.Context) {
si, ok := c.Get("session")
if !ok {
c.AbortWithStatusJSON(http.StatusUnauthorized, gin.H{"error": "Not authenticated"})
return
}
s := si.(session.Session)
uid := s.GetUserID()
us := user.GetUserService()
u, ok := us.GetUser(uid)
if !ok {
c.AbortWithStatusJSON(http.StatusUnauthorized, gin.H{"error": "No user found in the repository"})
return
}
// generate secret key
secret := uuid.New().String()
qrProps := QRProps{Secret: secret}
qrPropsJSON, err := json.Marshal(qrProps)
if err != nil {
c.AbortWithStatusJSON(http.StatusInternalServerError, gin.H{"error": "error updating user"})
return
}
if u.Properties == nil {
u.Properties = make(map[string]string)
}
u.Properties["passwordless.qr"] = string(qrPropsJSON)
err = us.UpdateUser(u)
if err != nil {
c.AbortWithStatusJSON(http.StatusInternalServerError, gin.H{"error": "error updating user"})
return
}
requestURI := middleware.GetRequestURI(c)
authURI := strings.ReplaceAll(requestURI, "/idm/otp/qr", "/service/otp/qr/login")
c.JSON(http.StatusOK, gin.H{"secret": secret, "userId": u.ID, "authURI": authURI})
}
func (pc *PasswordlessServicesController) AuthQR(c *gin.Context) {
var authQRRequest struct {
SID string `json:"sid"`
UID string `json:"uid"`
Realm string `json:"realm"`
Secret string `json:"secret"`
}
err := c.ShouldBindJSON(&authQRRequest)
if err != nil {
pc.logger.Warn("invalid request body", err)
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"error": "invalid request body"})
return
}
sess, err := session.GetSessionService().GetSession(authQRRequest.SID)
if err != nil {
c.AbortWithStatusJSON(http.StatusUnauthorized, gin.H{"error": "there is no valid authentication session"})
return
}
us := user.GetUserService()
u, ok := us.GetUser(authQRRequest.UID)
if !ok {
c.AbortWithStatusJSON(http.StatusUnauthorized, gin.H{"error": "error updating user"})
return
}
jsonProp, ok := u.Properties["passwordless.qr"]
if !ok {
c.AbortWithStatusJSON(http.StatusUnauthorized, gin.H{"error": "the user is not bound to QR"})
return
}
var qrProps QRProps
err = json.Unmarshal([]byte(jsonProp), &qrProps)
if err != nil {
pc.logger.Warn("AuthQR: the user is not bound to QR")
c.AbortWithStatusJSON(http.StatusUnauthorized, gin.H{"error": "the user is not bound to QR"})
return
}
if qrProps.Secret != authQRRequest.Secret {
pc.logger.Warn("AuthQR: user qr secrets does not match")
c.AbortWithStatusJSON(http.StatusUnauthorized, gin.H{"error": "the user is not bound to QR"})
return
}
// authorise session
var fs state.FlowState
err = json.Unmarshal([]byte(sess.Properties[constants.FlowStateSessionProperty]), &fs)
if err != nil {
c.AbortWithStatusJSON(http.StatusUnauthorized, gin.H{"error": "there is no valid authentication session"})
return
}
moduleFound := false
for _, m := range fs.Modules {
if m.Type == "qr" && m.Status == state.InProgress {
m.State["qrUserId"] = authQRRequest.UID
moduleFound = true
break
}
}
if !moduleFound {
pc.logger.Warn("AuthQR: no active qr module in the chain")
c.AbortWithStatusJSON(http.StatusUnauthorized, gin.H{"error": "there is no valid authentication session"})
return
}
fsJSON, err := json.Marshal(fs)
if err != nil {
c.AbortWithStatusJSON(http.StatusUnauthorized, gin.H{"error": "there is no valid authentication session"})
return
}
sess.Properties[constants.FlowStateSessionProperty] = string(fsJSON)
err = session.GetSessionService().UpdateSession(sess)
if err != nil {
c.AbortWithStatusJSON(http.StatusInternalServerError, gin.H{"error": err})
return
}
c.JSON(http.StatusOK, gin.H{"status": "success"})
}