Skip to content

Commit 15c4d5d

Browse files
committed
Update Readme.md
1 parent 22dc6ae commit 15c4d5d

File tree

1 file changed

+68
-5
lines changed

1 file changed

+68
-5
lines changed

README.md

Lines changed: 68 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -498,14 +498,14 @@ type CustomIdentityProvider struct {
498498
}
499499

500500
// RequestToken implements the IdentityProvider interface
501-
func (p *CustomIdentityProvider) RequestToken() (shared.IdentityProviderResponse, error) {
501+
func (p *CustomIdentityProvider) RequestToken(ctx context.Context) (shared.IdentityProviderResponse, error) {
502502
// Implement your custom token retrieval logic here
503503
// This could be calling your own auth service, using a different auth protocol, etc.
504504

505505
// For this example, we'll simulate getting a JWT token
506506
token := "your.jwt.token"
507507

508-
// Create a response using NewIDPResponse with RawToken type
508+
// Create a response using NewIDPResponse
509509
return shared.NewIDPResponse(shared.ResponseTypeRawToken, token)
510510
}
511511

@@ -677,22 +677,85 @@ A: You can create a custom identity provider by implementing the `IdentityProvid
677677
```go
678678
type IdentityProvider interface {
679679
// RequestToken requests a token from the identity provider.
680+
// The context is passed to the request to allow for cancellation and timeouts.
680681
// It returns the token, the expiration time, and an error if any.
681-
RequestToken() (IdentityProviderResponse, error)
682+
RequestToken(ctx context.Context) (IdentityProviderResponse, error)
682683
}
683684
```
684685

685-
The `IdentityProviderResponse` interface provides methods to access the authentication result:
686+
The response types are defined as constants:
686687
```go
688+
const (
689+
// ResponseTypeAuthResult is the type of the auth result.
690+
ResponseTypeAuthResult = "AuthResult"
691+
// ResponseTypeAccessToken is the type of the access token.
692+
ResponseTypeAccessToken = "AccessToken"
693+
// ResponseTypeRawToken is the type of the response when you have a raw string.
694+
ResponseTypeRawToken = "RawToken"
695+
)
696+
```
697+
698+
The `IdentityProviderResponse` interface and related interfaces provide methods to access the authentication result:
699+
```go
700+
// IdentityProviderResponse is the base interface that defines the type method
687701
type IdentityProviderResponse interface {
688-
// Type returns the type of the auth result
702+
// Type returns the type of identity provider response
689703
Type() string
704+
}
705+
706+
// AuthResultIDPResponse defines the method for getting the auth result
707+
type AuthResultIDPResponse interface {
690708
AuthResult() public.AuthResult
709+
}
710+
711+
// AccessTokenIDPResponse defines the method for getting the access token
712+
type AccessTokenIDPResponse interface {
691713
AccessToken() azcore.AccessToken
714+
}
715+
716+
// RawTokenIDPResponse defines the method for getting the raw token
717+
type RawTokenIDPResponse interface {
692718
RawToken() string
693719
}
694720
```
695721

722+
You can create a new response using the `NewIDPResponse` function:
723+
```go
724+
// NewIDPResponse creates a new auth result based on the type provided.
725+
// Type can be either AuthResult, AccessToken, or RawToken.
726+
// Second argument is the result of the type provided in the first argument.
727+
func NewIDPResponse(responseType string, result interface{}) (IdentityProviderResponse, error)
728+
```
729+
730+
Here's an example of how to use these types in a custom identity provider:
731+
```go
732+
type CustomIdentityProvider struct {
733+
tokenEndpoint string
734+
clientID string
735+
clientSecret string
736+
}
737+
738+
func (p *CustomIdentityProvider) RequestToken(ctx context.Context) (shared.IdentityProviderResponse, error) {
739+
// Get the token from your custom auth service
740+
token, err := p.getTokenFromCustomService()
741+
if err != nil {
742+
return nil, err
743+
}
744+
745+
// Create a response based on the token type
746+
switch token.Type {
747+
case "jwt":
748+
return shared.NewIDPResponse(shared.ResponseTypeRawToken, token.Value)
749+
case "access_token":
750+
return shared.NewIDPResponse(shared.ResponseTypeAccessToken, token.Value)
751+
case "auth_result":
752+
return shared.NewIDPResponse(shared.ResponseTypeAuthResult, token.Value)
753+
default:
754+
return nil, fmt.Errorf("unsupported token type: %s", token.Type)
755+
}
756+
}
757+
```
758+
696759
### Q: Can I customize how token responses are parsed?
697760
A: Yes, you can provide a custom `IdentityProviderResponseParser` in the `TokenManagerOptions`. This allows you to handle custom token formats or implement special parsing logic.
698761

0 commit comments

Comments
 (0)