From bb5cdb17dae64f62fc6c4c2e2f7f52f5d9015dd7 Mon Sep 17 00:00:00 2001 From: Connor Zanin <176337368+connor-strata@users.noreply.github.com> Date: Mon, 9 Dec 2024 19:05:02 -0700 Subject: [PATCH 1/5] Add the 'WithClientCredentialsResult' LoginOption --- idfabric/idfrabric.go | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/idfabric/idfrabric.go b/idfabric/idfrabric.go index d6a81ee..7162260 100644 --- a/idfabric/idfrabric.go +++ b/idfabric/idfrabric.go @@ -15,6 +15,11 @@ type IdentityProvider interface { type LoginOptions struct { Username string RedirectURL string + + // If ClientCredentialsResult is not nil, the client_credentials grnat type + // will be used for the Login method of this IdP. + // This callback will be called with the result of the Login() attempt. + ClientCredentialsResult func(*TokenResult, *error) } // LoginOpt allows for customizing the login experience. @@ -37,6 +42,18 @@ func WithRedirectURL(url string) LoginOpt { } } +type TokenResult struct{} + +// WithGrantTypeClientCredentials sets the grant type for requests to this IdP +// to 'client_credentials'. +// It sets the token and error result pointers to be +// populated by the Login() method. +func WithGrantTypeClientCredentials(f func(t *TokenResult, e *error)) LoginOpt { + return func(cfg *LoginOptions) { + cfg.ClientCredentialsResult = f + } +} + // AttributeProvider is used to retrieve attributes from an external system. A common // attribute provider would be a data store such as LDAP. type AttributeProvider interface { From e308944ffaf4509c955608bcd1f5cc7a86a02434 Mon Sep 17 00:00:00 2001 From: Connor Zanin <176337368+connor-strata@users.noreply.github.com> Date: Tue, 10 Dec 2024 08:02:55 -0700 Subject: [PATCH 2/5] Clean up godoc comments --- idfabric/idfrabric.go | 27 +++++++++++++++++---------- 1 file changed, 17 insertions(+), 10 deletions(-) diff --git a/idfabric/idfrabric.go b/idfabric/idfrabric.go index 7162260..5711414 100644 --- a/idfabric/idfrabric.go +++ b/idfabric/idfrabric.go @@ -2,6 +2,10 @@ package idfabric import "net/http" +const ( + GrantTypeCliendCredentials = iota + 1 +) + // IdentityProvider enables a way to interact with the identity provider. // Interactions may include login and logout. type IdentityProvider interface { @@ -16,10 +20,12 @@ type LoginOptions struct { Username string RedirectURL string - // If ClientCredentialsResult is not nil, the client_credentials grnat type - // will be used for the Login method of this IdP. - // This callback will be called with the result of the Login() attempt. - ClientCredentialsResult func(*TokenResult, *error) + GrantType int + + // ClientCredentialsResultCallback is called if GrantType is + // 'GrantTypeClientCredentials' and it is not nil. + // It is called at the end of Login() with the results of the + ClientCredentialsResultCallback func(*TokenResult, *error) } // LoginOpt allows for customizing the login experience. @@ -44,13 +50,14 @@ func WithRedirectURL(url string) LoginOpt { type TokenResult struct{} -// WithGrantTypeClientCredentials sets the grant type for requests to this IdP -// to 'client_credentials'. -// It sets the token and error result pointers to be -// populated by the Login() method. -func WithGrantTypeClientCredentials(f func(t *TokenResult, e *error)) LoginOpt { +// WithGrantTypeClientCredentials sets the grant type for this Login attempt to +// 'client_credentials'. +// The provided callback is called at the end of the Login() routine with the +// results. +func WithGrantTypeClientCredentials(callback func(t *TokenResult, e *error)) LoginOpt { return func(cfg *LoginOptions) { - cfg.ClientCredentialsResult = f + cfg.GrantType = GrantTypeCliendCredentials + cfg.ClientCredentialsResultCallback = callback } } From d68a28524543d8c4293c6fa7790100157899278b Mon Sep 17 00:00:00 2001 From: Connor Zanin <176337368+connor-strata@users.noreply.github.com> Date: Tue, 10 Dec 2024 08:06:40 -0700 Subject: [PATCH 3/5] Fix typo --- idfabric/idfrabric.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/idfabric/idfrabric.go b/idfabric/idfrabric.go index 5711414..9316d2c 100644 --- a/idfabric/idfrabric.go +++ b/idfabric/idfrabric.go @@ -3,7 +3,7 @@ package idfabric import "net/http" const ( - GrantTypeCliendCredentials = iota + 1 + GrantTypeClientCredentials = iota + 1 ) // IdentityProvider enables a way to interact with the identity provider. @@ -56,7 +56,7 @@ type TokenResult struct{} // results. func WithGrantTypeClientCredentials(callback func(t *TokenResult, e *error)) LoginOpt { return func(cfg *LoginOptions) { - cfg.GrantType = GrantTypeCliendCredentials + cfg.GrantType = GrantTypeClientCredentials cfg.ClientCredentialsResultCallback = callback } } From 922ee650591743e2e1624805e239738d21885acd Mon Sep 17 00:00:00 2001 From: Connor Zanin <176337368+connor-strata@users.noreply.github.com> Date: Tue, 10 Dec 2024 08:21:07 -0700 Subject: [PATCH 4/5] Use pointers instead of a callback --- idfabric/idfrabric.go | 17 +++++++---------- 1 file changed, 7 insertions(+), 10 deletions(-) diff --git a/idfabric/idfrabric.go b/idfabric/idfrabric.go index 9316d2c..1af026d 100644 --- a/idfabric/idfrabric.go +++ b/idfabric/idfrabric.go @@ -20,12 +20,9 @@ type LoginOptions struct { Username string RedirectURL string - GrantType int - - // ClientCredentialsResultCallback is called if GrantType is - // 'GrantTypeClientCredentials' and it is not nil. - // It is called at the end of Login() with the results of the - ClientCredentialsResultCallback func(*TokenResult, *error) + GrantType int + ErrorResult *error + TokenResult *TokenResult } // LoginOpt allows for customizing the login experience. @@ -52,12 +49,12 @@ type TokenResult struct{} // WithGrantTypeClientCredentials sets the grant type for this Login attempt to // 'client_credentials'. -// The provided callback is called at the end of the Login() routine with the -// results. -func WithGrantTypeClientCredentials(callback func(t *TokenResult, e *error)) LoginOpt { +// The results of Login will be stored in tokenResult and errorResult. +func WithGrantTypeClientCredentials(tokenResult *TokenResult, errorResult *error) LoginOpt { return func(cfg *LoginOptions) { cfg.GrantType = GrantTypeClientCredentials - cfg.ClientCredentialsResultCallback = callback + cfg.TokenResult = tokenResult + cfg.ErrorResult = errorResult } } From 0886bbd82165f896e1ebaf1ee975c57da7a3eb51 Mon Sep 17 00:00:00 2001 From: Elias Friedman <7999104+eliasjf@users.noreply.github.com> Date: Wed, 11 Dec 2024 12:05:02 -0800 Subject: [PATCH 5/5] Update 'WithGrantTypeClientCredentials' to consume struct as input --- idfabric/idfrabric.go | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) diff --git a/idfabric/idfrabric.go b/idfabric/idfrabric.go index 1af026d..a49a774 100644 --- a/idfabric/idfrabric.go +++ b/idfabric/idfrabric.go @@ -21,8 +21,7 @@ type LoginOptions struct { RedirectURL string GrantType int - ErrorResult *error - TokenResult *TokenResult + LoginResult *LoginResult } // LoginOpt allows for customizing the login experience. @@ -45,16 +44,24 @@ func WithRedirectURL(url string) LoginOpt { } } -type TokenResult struct{} +type LoginResult struct { + TokenResult + Error error +} + +type TokenResult struct { + AccessToken string + Scope string + ExpiresIn int +} // WithGrantTypeClientCredentials sets the grant type for this Login attempt to // 'client_credentials'. // The results of Login will be stored in tokenResult and errorResult. -func WithGrantTypeClientCredentials(tokenResult *TokenResult, errorResult *error) LoginOpt { +func WithGrantTypeClientCredentials(result *LoginResult) LoginOpt { return func(cfg *LoginOptions) { cfg.GrantType = GrantTypeClientCredentials - cfg.TokenResult = tokenResult - cfg.ErrorResult = errorResult + cfg.LoginResult = result } }