This repository has been archived by the owner on Jan 21, 2025. 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.
feat: authentication persistent storage GO (#91)
- Loading branch information
1 parent
acba4f1
commit deae892
Showing
29 changed files
with
514 additions
and
175 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
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
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
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
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,77 @@ | ||
package auth | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
|
||
"github.com/konstellation-io/kai-sdk/go-sdk/internal/common" | ||
|
||
"github.com/Nerzal/gocloak/v13" | ||
"github.com/go-logr/logr" | ||
"github.com/spf13/viper" | ||
) | ||
|
||
type Auth struct { | ||
logger logr.Logger | ||
authEndpoint string | ||
clientID string | ||
clientSecret string | ||
realm string | ||
username string | ||
password string | ||
jwt *gocloak.JWT | ||
} | ||
|
||
func New(logger logr.Logger) *Auth { | ||
user := viper.GetString(common.ConfigMinioClientUserKey) | ||
password := viper.GetString(common.ConfigMinioClientPasswordKey) | ||
authEndpoint := viper.GetString(common.ConfigAuthEndpointKey) | ||
realm := viper.GetString(common.ConfigAuthRealmKey) | ||
clientID := viper.GetString(common.ConfigAuthClientKey) | ||
clientSecret := viper.GetString(common.ConfigAuthClientSecretKey) | ||
|
||
return &Auth{ | ||
logger: logger, | ||
authEndpoint: authEndpoint, | ||
clientID: clientID, | ||
clientSecret: clientSecret, | ||
realm: realm, | ||
username: user, | ||
password: password, | ||
jwt: nil, | ||
} | ||
} | ||
|
||
func (a *Auth) GetToken() (*gocloak.JWT, error) { | ||
client := gocloak.NewClient(a.authEndpoint) | ||
ctx := context.Background() | ||
|
||
if a.jwt != nil { | ||
token, err := client.RefreshToken(ctx, a.jwt.RefreshToken, a.clientID, a.clientSecret, a.realm) | ||
|
||
if err != nil { | ||
a.logger.V(2).Info("Couldn't refresh token") | ||
} else { | ||
a.jwt = token | ||
|
||
return token, nil | ||
} | ||
} | ||
|
||
token, err := client.Login( | ||
ctx, | ||
a.clientID, | ||
a.clientSecret, | ||
a.realm, | ||
a.username, | ||
a.password, | ||
) | ||
if err != nil { | ||
a.logger.Info(fmt.Sprintf("Error getting token: %s", err.Error())) | ||
return nil, err | ||
} | ||
|
||
a.jwt = token | ||
|
||
return a.jwt, 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
Oops, something went wrong.