-
Notifications
You must be signed in to change notification settings - Fork 0
/
is_user.go
61 lines (45 loc) · 1.39 KB
/
is_user.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
package middlewares
import (
"encoding/base64"
"strings"
"github.com/ahmedaly113/eltra-api/src/helpers"
"github.com/ahmedaly113/eltra-api/src/libs/fail"
"github.com/ahmedaly113/eltra-api/src/libs/rpc"
"github.com/ahmedaly113/eltra-api/src/models"
"github.com/gin-gonic/gin"
)
var userTokenModel = new(models.UserToken)
// IsUser checks the user Authorization header.
func IsUser() gin.HandlerFunc {
return func(c *gin.Context) {
authorization := c.GetHeader("Authorization")
if len(authorization) < 7 || !strings.HasPrefix(authorization, "Basic ") {
fail.AnswerCustom(c, fail.Unauthorized, "")
return
}
authorizationValue := helpers.Substring(authorization, 6)
credentials, err := base64.StdEncoding.DecodeString(authorizationValue)
if err != nil {
fail.AnswerCustom(c, fail.Unauthorized, "")
return
}
credentialsParts := strings.Split(string(credentials), ":")
if len(credentialsParts) != 2 {
fail.AnswerCustom(c, fail.Unauthorized, "")
return
}
purseHash := credentialsParts[0]
signature := credentialsParts[1]
userToken, err := userTokenModel.GetByPurseHash(purseHash)
if err != nil {
fail.AnswerCustom(c, fail.Unauthorized, "")
return
}
res, err := rpc.VerifyMessage(purseHash, signature, userToken.Challenge)
if err != nil || !res.Result {
fail.AnswerCustom(c, fail.Unauthorized, "")
return
}
c.Set("purseHash", purseHash)
}
}