-
Notifications
You must be signed in to change notification settings - Fork 2
/
auth_context.go
52 lines (42 loc) · 1.18 KB
/
auth_context.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
package examples
import (
"context"
"fmt"
"github.com/Microkubes/microservice-security/auth"
)
// SetAuthInContext sets an auth.Auth object in context and checks for it
func SetAuthInContext() error {
ctx := context.Background()
authObj := auth.Auth{
UserID: "e027a023-281c-424a-963a-27a32a7b0695",
Username: "test-user",
Roles: []string{"user", "admin"},
Organizations: []string{"Jormungandr"},
}
ctx = auth.SetAuth(ctx, &authObj)
hasAuth := auth.HasAuth(ctx)
if hasAuth {
fmt.Println("Auth has been set in context.")
} else {
return fmt.Errorf("Auth has not been set")
}
return nil
}
// RetrieveAuthFromContext first stores an Auth object in context, then retrieves it
// via auth.GetAuth
func RetrieveAuthFromContext() error {
ctx := context.Background()
authObj := auth.Auth{
UserID: "e027a023-281c-424a-963a-27a32a7b0695",
Username: "test-user",
Roles: []string{"user", "admin"},
Organizations: []string{"Jormungandr"},
}
ctx = auth.SetAuth(ctx, &authObj)
retrieved := auth.GetAuth(ctx)
if retrieved == nil {
return fmt.Errorf("No auth in context")
}
fmt.Println("Auth retrieved from context.")
return nil
}