-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathauthenticator_examples_test.go
57 lines (47 loc) · 1.17 KB
/
authenticator_examples_test.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
// SPDX-FileCopyrightText: 2024 Comcast Cable Communications Management, LLC
// SPDX-License-Identifier: Apache-2.0
package bascule
import (
"context"
"fmt"
)
type Extra struct {
Name string
Age int
}
func (e Extra) Principal() string { return e.Name }
// ExampleJoinTokens_augment shows how to augment a Token as part
// of authentication workflow.
func ExampleJoinTokens_augment() {
original := StubToken("original")
authenticator, _ := NewAuthenticator[string](
WithTokenParsers(
StubTokenParser[string]{
Token: original,
},
),
WithValidators(
AsValidator[string](
func(t Token) (Token, error) {
// augment this token with extra information
return JoinTokens(t, Extra{Name: "extra", Age: 33}), nil
},
),
),
)
authenticated, _ := authenticator.Authenticate(
context.Background(),
"source",
)
fmt.Println("authenticated principal:", authenticated.Principal())
var extra Extra
if !TokenAs(authenticated, &extra) {
panic("token cannot be converted")
}
fmt.Println("extra.Name:", extra.Name)
fmt.Println("extra.Age:", extra.Age)
// Output:
// authenticated principal: original
// extra.Name: extra
// extra.Age: 33
}