Skip to content

Commit

Permalink
working example of augmenting a token
Browse files Browse the repository at this point in the history
  • Loading branch information
johnabass committed Aug 26, 2024
1 parent c826a49 commit 69196cf
Showing 1 changed file with 57 additions and 0 deletions.
57 changes: 57 additions & 0 deletions authenticator_examples_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,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
}

0 comments on commit 69196cf

Please sign in to comment.