@@ -31,12 +31,8 @@ import (
3131 "os"
3232 "strings"
3333
34- httptransport "github.com/go-openapi/runtime/client"
35- "github.com/go-openapi/strfmt"
3634 "github.com/gorilla/sessions"
37- hydra "github.com/ory/hydra-client-go/client"
38- hydraAdmin "github.com/ory/hydra-client-go/client/admin"
39- hydraModels "github.com/ory/hydra-client-go/models"
35+ hydra "github.com/ory/hydra-client-go/v2"
4036 kratos "github.com/ory/kratos-client-go"
4137 log "github.com/sirupsen/logrus"
4238 "github.com/spf13/pflag"
@@ -89,10 +85,10 @@ type HydraKratosConfig struct {
8985const HydraLoginStateKey string = "hydra_login_state"
9086
9187type hydraAdminClientService interface {
92- AcceptConsentRequest ( params * hydraAdmin. AcceptConsentRequestParams ) ( * hydraAdmin. AcceptConsentRequestOK , error )
93- AcceptLoginRequest ( params * hydraAdmin. AcceptLoginRequestParams ) ( * hydraAdmin. AcceptLoginRequestOK , error )
94- GetConsentRequest ( params * hydraAdmin. GetConsentRequestParams ) ( * hydraAdmin. GetConsentRequestOK , error )
95- IntrospectOAuth2Token (params * hydraAdmin. IntrospectOAuth2TokenParams ) ( * hydraAdmin. IntrospectOAuth2TokenOK , error )
88+ AcceptOAuth2ConsentRequest (context. Context ) hydra. OAuth2APIAcceptOAuth2ConsentRequestRequest
89+ AcceptOAuth2LoginRequest (context. Context ) hydra. OAuth2APIAcceptOAuth2LoginRequestRequest
90+ GetOAuth2ConsentRequest (context. Context ) hydra. OAuth2APIGetOAuth2ConsentRequestRequest
91+ IntrospectOAuth2Token (context. Context ) hydra. OAuth2APIIntrospectOAuth2TokenRequest
9692}
9793
9894type kratosPublicClientService interface {
@@ -142,17 +138,18 @@ func createHTTPClient() (*http.Client, error) {
142138 return client , nil
143139}
144140
145- func createRuntime ( path string , client * http.Client ) (* httptransport. Runtime , error ) {
146- u , err := url .Parse (path )
141+ func createHydraClient ( host string , client * http.Client ) (* hydra. APIClient , error ) {
142+ u , err := url .Parse (host )
147143 if err != nil {
148144 return nil , err
149145 }
150- return httptransport .NewWithClient (
151- u .Host ,
152- u .Path ,
153- []string {u .Scheme },
154- client ,
155- ), nil
146+
147+ conf := hydra .NewConfiguration ()
148+ conf .Host = u .Host
149+ conf .Scheme = u .Scheme
150+ conf .Servers = hydra.ServerConfigurations {{URL : host }}
151+ conf .HTTPClient = client
152+ return hydra .NewAPIClient (conf ), nil
156153}
157154
158155func createKratosClient (host string , client * http.Client ) (* kratos.APIClient , error ) {
@@ -180,12 +177,10 @@ func NewHydraKratosClientFromConfig(cfg *HydraKratosConfig) (*HydraKratosClient,
180177 }
181178 }
182179
183- hydraAdminRuntime , err := createRuntime (cfg .HydraAdminHost , httpClient )
180+ hydraAdminClient , err := createHydraClient (cfg .HydraAdminHost , httpClient )
184181 if err != nil {
185182 return nil , err
186183 }
187- // We specify the Admin client to avoid confusing bugs because the Public client is held behind a different endpoint.
188- hydraAdminClient := hydra .New (hydraAdminRuntime , strfmt .NewFormats ()).Admin
189184
190185 // One can theoretically send public requests to the Admin Host but then kratos will
191186 // 302 the requests to the public host/port.
@@ -205,7 +200,7 @@ func NewHydraKratosClientFromConfig(cfg *HydraKratosConfig) (*HydraKratosClient,
205200 return & HydraKratosClient {
206201 Config : cfg ,
207202 httpClient : httpClient ,
208- hydraAdminClient : hydraAdminClient ,
203+ hydraAdminClient : hydraAdminClient . OAuth2API ,
209204 kratosAdminClient : kratosAdminClient .IdentityAPI ,
210205 kratosPublicClient : kratosPublicClient .FrontendAPI ,
211206 }, nil
@@ -374,19 +369,14 @@ type RedirectResponse struct {
374369// AcceptHydraLogin sends a request to accept the login on the hydra endpoint.
375370func (c * HydraKratosClient ) AcceptHydraLogin (ctx context.Context , challenge string , whoamiResp * Whoami ) (* RedirectResponse , error ) {
376371 subject := whoamiResp .ID ()
377- params := & hydraAdmin.AcceptLoginRequestParams {
378- Body : & hydraModels.AcceptLoginRequest {
379- Context : whoamiResp .kratosSession ,
380- Subject : & subject ,
381- },
382- LoginChallenge : challenge ,
383- Context : ctx ,
384- }
385- resp , err := c .hydraAdminClient .AcceptLoginRequest (params )
372+ body := hydra .NewAcceptOAuth2LoginRequest (subject )
373+ body .SetContext (whoamiResp .kratosSession )
374+
375+ resp , _ , err := c .hydraAdminClient .AcceptOAuth2LoginRequest (ctx ).LoginChallenge (challenge ).AcceptOAuth2LoginRequest (* body ).Execute ()
386376 if err != nil {
387377 return nil , err
388378 }
389- return & RedirectResponse {RedirectTo : resp . GetPayload () .RedirectTo }, nil
379+ return & RedirectResponse {RedirectTo : & resp .RedirectTo }, nil
390380}
391381
392382// InterceptHydraUserConsent performs the user consent flow bypassing normal user interaction. Hydra uses
@@ -432,43 +422,35 @@ func (c *HydraKratosClient) AcceptConsent(ctx context.Context, challenge string)
432422 if challenge == "" {
433423 return nil , fmt .Errorf ("challenge is empty" )
434424 }
435- resp , err := c .hydraAdminClient .GetConsentRequest (& hydraAdmin.GetConsentRequestParams {
436- ConsentChallenge : challenge ,
437- Context : ctx ,
438- })
425+ resp , _ , err := c .hydraAdminClient .GetOAuth2ConsentRequest (ctx ).ConsentChallenge (challenge ).Execute ()
439426 if err != nil {
440427 log .Debug ("error on hydra.consentRequest:" )
441428 return nil , err
442429 }
443430
444- if resp . GetPayload () == nil {
431+ if resp == nil {
445432 log .Debug ("consent request payload is empty" )
446- return nil , err
433+ return nil , fmt . Errorf ( "consent request payload is empty" )
447434 }
448435
449- consentRequest := resp .GetPayload ()
450-
451436 // We only trust the client that's passed in as a config here. In the future we might want to support other clients
452437 // at which point we will want to actually ask for permission from the user.
453438
454439 // TODO(ddelnano): This needs cannot be hard coded to auth-code-client, but should be set in the config.
455- // if consentRequest .Client.ClientID != c.Config.HydraClientID {
456- // return nil, fmt.Errorf("'%s' not an allowed client", consentRequest .Client.ClientID )
440+ // if resp .Client.ClientId != c.Config.HydraClientID {
441+ // return nil, fmt.Errorf("'%s' not an allowed client", resp .Client.ClientId )
457442 // }
458443
459- acceptResp , err := c .hydraAdminClient .AcceptConsentRequest (& hydraAdmin.AcceptConsentRequestParams {
460- Body : & hydraModels.AcceptConsentRequest {
461- GrantScope : consentRequest .RequestedScope ,
462- GrantAccessTokenAudience : consentRequest .RequestedAccessTokenAudience ,
463- },
464- ConsentChallenge : challenge ,
465- Context : ctx ,
466- })
444+ body := hydra .NewAcceptOAuth2ConsentRequest ()
445+ body .SetGrantScope (resp .RequestedScope )
446+ body .SetGrantAccessTokenAudience (resp .RequestedAccessTokenAudience )
447+
448+ acceptResp , _ , err := c .hydraAdminClient .AcceptOAuth2ConsentRequest (ctx ).ConsentChallenge (challenge ).AcceptOAuth2ConsentRequest (* body ).Execute ()
467449 if err != nil {
468450 log .Debug ("error on hydra.AcceptConsentRequest:" )
469451 return nil , err
470452 }
471- return & RedirectResponse {RedirectTo : acceptResp . GetPayload () .RedirectTo }, nil
453+ return & RedirectResponse {RedirectTo : & acceptResp .RedirectTo }, nil
472454}
473455
474456// HandleLogin handles the login for Hydra and Kratos.
@@ -554,16 +536,15 @@ func (c *HydraKratosClient) SessionKey() string {
554536
555537// GetUserIDFromToken returns the userID from the subject portion of the access token.
556538func (c * HydraKratosClient ) GetUserIDFromToken (ctx context.Context , token string ) (string , error ) {
557- params := & hydraAdmin.IntrospectOAuth2TokenParams {
558- Context : ctx ,
559- Token : token ,
560- }
561- res , err := c .hydraAdminClient .IntrospectOAuth2Token (params )
539+ res , _ , err := c .hydraAdminClient .IntrospectOAuth2Token (ctx ).Token (token ).Execute ()
562540 if err != nil {
563541 return "" , err
564542 }
565543
566- return res .GetPayload ().Sub , nil
544+ if res .Sub == nil {
545+ return "" , fmt .Errorf ("token introspection returned nil subject" )
546+ }
547+ return * res .Sub , nil
567548}
568549
569550// KratosUserInfo contains the user information format as stored in Kratos.
0 commit comments