-
Notifications
You must be signed in to change notification settings - Fork 0
/
auth.go
53 lines (41 loc) · 1.29 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
// Copyright (C) 2021 Creditor Corp. Group.
// See LICENSE for copying information.
package goauth
import (
"context"
"github.com/zeebo/errs"
)
// Error is an error class for auth errors.
var Error = errs.Class("auth error")
// Key is an enumeration for different auth keys for context.
type Key int
const (
// KeyClaims is a key to receive auth result from context - claims or error.
KeyClaims Key = 0
// KeyToken is a key to receive auth token from context.
KeyToken Key = 1
)
// SetClaims creates new context with Claims.
func SetClaims(ctx context.Context, claims Claims) context.Context {
return context.WithValue(ctx, KeyClaims, claims)
}
// GetClaims gets Claims from context.
func GetClaims(ctx context.Context) (Claims, error) {
value := ctx.Value(KeyClaims)
if auth, ok := value.(Claims); ok {
return auth, nil
}
if err, ok := value.(error); ok {
return Claims{}, Error.Wrap(err)
}
return Claims{}, Error.New("could not get auth or error from context")
}
// SetToken creates context with auth token.
func SetToken(ctx context.Context, key []byte) context.Context {
return context.WithValue(ctx, KeyToken, key)
}
// GetToken returns auth token from context is exists.
func GetToken(ctx context.Context) ([]byte, bool) {
key, ok := ctx.Value(KeyToken).([]byte)
return key, ok
}