@@ -498,14 +498,14 @@ type CustomIdentityProvider struct {
498
498
}
499
499
500
500
// RequestToken implements the IdentityProvider interface
501
- func (p *CustomIdentityProvider ) RequestToken () (shared .IdentityProviderResponse , error ) {
501
+ func (p *CustomIdentityProvider ) RequestToken (ctx context . Context ) (shared .IdentityProviderResponse , error ) {
502
502
// Implement your custom token retrieval logic here
503
503
// This could be calling your own auth service, using a different auth protocol, etc.
504
504
505
505
// For this example, we'll simulate getting a JWT token
506
506
token := " your.jwt.token"
507
507
508
- // Create a response using NewIDPResponse with RawToken type
508
+ // Create a response using NewIDPResponse
509
509
return shared.NewIDPResponse (shared.ResponseTypeRawToken , token)
510
510
}
511
511
@@ -677,22 +677,85 @@ A: You can create a custom identity provider by implementing the `IdentityProvid
677
677
``` go
678
678
type IdentityProvider interface {
679
679
// RequestToken requests a token from the identity provider.
680
+ // The context is passed to the request to allow for cancellation and timeouts.
680
681
// It returns the token, the expiration time, and an error if any.
681
- RequestToken () (IdentityProviderResponse, error )
682
+ RequestToken (ctx context. Context ) (IdentityProviderResponse, error )
682
683
}
683
684
```
684
685
685
- The ` IdentityProviderResponse ` interface provides methods to access the authentication result :
686
+ The response types are defined as constants :
686
687
``` 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
687
701
type IdentityProviderResponse interface {
688
- // Type returns the type of the auth result
702
+ // Type returns the type of identity provider response
689
703
Type () string
704
+ }
705
+
706
+ // AuthResultIDPResponse defines the method for getting the auth result
707
+ type AuthResultIDPResponse interface {
690
708
AuthResult () public.AuthResult
709
+ }
710
+
711
+ // AccessTokenIDPResponse defines the method for getting the access token
712
+ type AccessTokenIDPResponse interface {
691
713
AccessToken () azcore.AccessToken
714
+ }
715
+
716
+ // RawTokenIDPResponse defines the method for getting the raw token
717
+ type RawTokenIDPResponse interface {
692
718
RawToken () string
693
719
}
694
720
```
695
721
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
+
696
759
### Q: Can I customize how token responses are parsed?
697
760
A: Yes, you can provide a custom ` IdentityProviderResponseParser ` in the ` TokenManagerOptions ` . This allows you to handle custom token formats or implement special parsing logic.
698
761
0 commit comments