This repository has been archived by the owner on Dec 11, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
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 #1 from G-PORTAL/more-dynamic-auth-options
Add more dynamic options to overwrite client settings
- Loading branch information
Showing
6 changed files
with
164 additions
and
84 deletions.
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
package auth | ||
|
||
const hostnameDefault = "https://auth.g-portal.com/auth" | ||
const realmDefault = "master" |
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 @@ | ||
/* This authentication method is not fully implemented (yet) | ||
but will be used as long life access token replacement later on */ | ||
|
||
package auth | ||
|
||
import ( | ||
"context" | ||
"time" | ||
|
||
"github.com/Nerzal/gocloak/v12" | ||
) | ||
|
||
type ProviderKeycloakClientAuth struct { | ||
ClientID string | ||
ClientSecret string | ||
Hostname *string | ||
Realm *string | ||
|
||
tokenData *struct { | ||
Token *gocloak.JWT | ||
Expires time.Time | ||
} | ||
} | ||
|
||
func (p *ProviderKeycloakClientAuth) refresh() error { | ||
hostname := hostnameDefault | ||
if p.Hostname != nil { | ||
hostname = *p.Hostname | ||
} | ||
realm := realmDefault | ||
if p.Realm != nil { | ||
realm = *p.Realm | ||
} | ||
token, err := gocloak.NewClient(hostname).GetToken(context.Background(), realm, gocloak.TokenOptions{ | ||
ClientID: &p.ClientID, | ||
ClientSecret: &p.ClientSecret, | ||
GrantType: gocloak.StringP("client_credentials"), | ||
}) | ||
if err != nil { | ||
p.tokenData = nil | ||
return err | ||
} | ||
p.tokenData = &struct { | ||
Token *gocloak.JWT | ||
Expires time.Time | ||
}{Token: token, Expires: time.Now().Add(time.Duration(token.ExpiresIn-10) * time.Second)} | ||
return nil | ||
} | ||
|
||
func (p *ProviderKeycloakClientAuth) GetToken(ctx context.Context) (string, error) { | ||
if p.tokenData == nil || p.tokenData.Expires.Before(time.Now()) { | ||
if err := p.refresh(); err != nil { | ||
return "", err | ||
} | ||
} | ||
return p.tokenData.Token.AccessToken, nil | ||
} |
64 changes: 64 additions & 0 deletions
64
pkg/gpcloud/client/auth/auth_keycloak_username_password.go
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,64 @@ | ||
package auth | ||
|
||
import ( | ||
"context" | ||
"time" | ||
|
||
"github.com/Nerzal/gocloak/v12" | ||
) | ||
|
||
type ProviderKeycloakUserPassword struct { | ||
ClientID string | ||
ClientSecret string | ||
Username string | ||
Password string | ||
Hostname *string | ||
Realm *string | ||
|
||
tokenData *struct { | ||
Token *gocloak.JWT | ||
Expires time.Time | ||
} | ||
} | ||
|
||
func (p *ProviderKeycloakUserPassword) refresh() error { | ||
hostname := hostnameDefault | ||
if p.Hostname != nil { | ||
hostname = *p.Hostname | ||
} | ||
realm := realmDefault | ||
if p.Realm != nil { | ||
realm = *p.Realm | ||
} | ||
if p.tokenData == nil { | ||
token, err := gocloak.NewClient(hostname).Login(context.Background(), p.ClientID, p.ClientSecret, realm, p.Username, p.Password) | ||
if err != nil { | ||
p.tokenData = nil | ||
return err | ||
} | ||
p.tokenData = &struct { | ||
Token *gocloak.JWT | ||
Expires time.Time | ||
}{Token: token, Expires: time.Now().Add(time.Duration(token.ExpiresIn-10) * time.Second)} | ||
} else { | ||
token, err := gocloak.NewClient(hostname).RefreshToken(context.Background(), p.tokenData.Token.RefreshToken, p.ClientID, p.ClientSecret, realm) | ||
if err != nil { | ||
p.tokenData = nil | ||
return err | ||
} | ||
p.tokenData = &struct { | ||
Token *gocloak.JWT | ||
Expires time.Time | ||
}{Token: token, Expires: time.Now().Add(time.Duration(token.ExpiresIn-10) * time.Second)} | ||
} | ||
return nil | ||
} | ||
|
||
func (p *ProviderKeycloakUserPassword) GetToken(ctx context.Context) (string, error) { | ||
if p.tokenData == nil || p.tokenData.Expires.Before(time.Now()) { | ||
if err := p.refresh(); err != nil { | ||
return "", err | ||
} | ||
} | ||
return p.tokenData.Token.AccessToken, nil | ||
} |
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