-
Notifications
You must be signed in to change notification settings - Fork 0
/
auth.go
87 lines (78 loc) · 2.37 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
package auth
import (
"fmt"
"os"
"time"
"github.com/golang-jwt/jwt"
"github.com/snowmerak/dakuaz"
"github.com/snowmerak/rest-make/cookie"
"github.com/snowmerak/rest-make/cookie/jwthmac"
"github.com/valyala/fasthttp"
)
const token = "token"
var secret string
func init() {
sec := os.Getenv("JWT_SECRET")
if sec == "" {
secret = "1q!2w@3e#4r$5t%6y^7u&8i*9o(0p)_+qazxswedc"
}
secret = sec
}
func JWT(ctx *fasthttp.RequestCtx, criteria func(jwt.MapClaims) bool) error {
ck := ctx.Request.Header.Cookie(token)
if ck == nil {
ctx.Response.Header.SetStatusCode(fasthttp.StatusUnauthorized)
return fmt.Errorf("no token")
}
claims, err := jwthmac.Deserialize(secret, string(ck))
if err != nil {
ctx.Response.Header.SetStatusCode(fasthttp.StatusUnauthorized)
return fmt.Errorf("auth.JWT: %w", err)
}
if !criteria(claims) {
ctx.Response.Header.SetStatusCode(fasthttp.StatusForbidden)
return fmt.Errorf("token is disqualified")
}
ctx.Response.Header.SetStatusCode(fasthttp.StatusOK)
return nil
}
func Dakuaz(ctx *fasthttp.RequestCtx, criteria func(*dakuaz.Dakuaz) bool) error {
ck := ctx.Request.Header.Cookie(token)
if ck == nil {
ctx.Response.Header.SetStatusCode(fasthttp.StatusUnauthorized)
return fmt.Errorf("no token")
}
d, err := dakuaz.Desirialize(string(ck))
if err != nil {
ctx.Response.Header.SetStatusCode(fasthttp.StatusUnauthorized)
return fmt.Errorf("auth.Dakuaz: %w", err)
}
if d.Verify(secret) != nil {
ctx.Response.Header.SetStatusCode(fasthttp.StatusUnauthorized)
return fmt.Errorf("token is disqualified")
}
if !criteria(d) {
ctx.Response.Header.SetStatusCode(fasthttp.StatusForbidden)
return fmt.Errorf("token is disqualified")
}
ctx.Response.Header.SetStatusCode(fasthttp.StatusOK)
return nil
}
func MakeJWT(ctx *fasthttp.RequestCtx, claims jwt.StandardClaims, additional jwt.MapClaims) error {
tk, err := jwthmac.Serialize(secret, claims, additional)
if err != nil {
return fmt.Errorf("auth.MakeJWT: %w", err)
}
ck := cookie.New(token, tk, time.Unix(claims.ExpiresAt, 0).UTC())
ctx.Response.Header.SetCookie(ck)
return nil
}
func MakeDakuaz(ctx *fasthttp.RequestCtx, id string, duration time.Duration) error {
tk, err := dakuaz.New(secret, id, duration)
if err != nil {
return fmt.Errorf("auth.MakeDakuaz: %w", err)
}
ck := cookie.New(token, tk.Serialize(), time.Now().Add(duration))
ctx.Response.Header.SetCookie(ck)
return nil
}