-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #289 from xmidt-org/feature/token-wrapping
Feature/token wrapping
- Loading branch information
Showing
4 changed files
with
465 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.