Skip to content

Commit

Permalink
Add central function to persist users on Login
Browse files Browse the repository at this point in the history
  • Loading branch information
moebiusband73 committed Mar 28, 2024
1 parent 50401e0 commit 6828c97
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 15 deletions.
4 changes: 2 additions & 2 deletions cmd/cc-backend/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -347,7 +347,7 @@ func main() {
info := map[string]interface{}{}
info["hasOpenIDConnect"] = false

if config.Keys.OpenIDProvider != "" {
if config.Keys.OpenIDConfig != nil {
openIDConnect := auth.NewOIDC(authentication)
openIDConnect.RegisterEndpoints(r)
info["hasOpenIDConnect"] = true
Expand Down Expand Up @@ -569,8 +569,8 @@ func main() {
}

var cfg struct {
Compression int `json:"compression"`
Retention schema.Retention `json:"retention"`
Compression int `json:"compression"`
}

cfg.Retention.IncludeDB = true
Expand Down
13 changes: 13 additions & 0 deletions internal/auth/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,19 @@ func Init() (*Authentication, error) {
return auth, nil
}

func persistUser(user *schema.User) {
r := repository.GetUserRepository()
_, err := r.GetUser(user.Username)

if err != nil && err != sql.ErrNoRows {
log.Errorf("Error while loading user '%s': %v", user.Username, err)
} else if err == sql.ErrNoRows {
if err := r.AddUser(user); err != nil {
log.Errorf("Error while adding user '%s' to DB: %v", user.Username, err)
}
}
}

func (auth *Authentication) SaveSession(rw http.ResponseWriter, r *http.Request, user *schema.User) error {
session, err := auth.sessionStore.New(r, "session")
if err != nil {
Expand Down
4 changes: 1 addition & 3 deletions internal/auth/jwtCookieSession.go
Original file line number Diff line number Diff line change
Expand Up @@ -199,9 +199,7 @@ func (ja *JWTCookieSessionAuthenticator) Login(
}

if jc.SyncUserOnLogin {
if err := repository.GetUserRepository().AddUser(user); err != nil {
log.Errorf("Error while adding user '%s' to DB", user.Username)
}
persistUser(user)
}
}

Expand Down
4 changes: 1 addition & 3 deletions internal/auth/jwtSession.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,9 +139,7 @@ func (ja *JWTSessionAuthenticator) Login(
}

if config.Keys.JwtConfig.SyncUserOnLogin {
if err := repository.GetUserRepository().AddUser(user); err != nil {
log.Errorf("Error while adding user '%s' to DB", user.Username)
}
persistUser(user)
}
}

Expand Down
13 changes: 11 additions & 2 deletions internal/auth/oidc.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ func setCallbackCookie(w http.ResponseWriter, r *http.Request, name, value strin
}

func NewOIDC(a *Authentication) *OIDC {
provider, err := oidc.NewProvider(context.Background(), config.Keys.OpenIDProvider)
provider, err := oidc.NewProvider(context.Background(), config.Keys.OpenIDConfig.Provider)
if err != nil {
log.Fatal(err)
}
Expand Down Expand Up @@ -89,6 +89,10 @@ func (oa *OIDC) OAuth2Callback(rw http.ResponseWriter, r *http.Request) {
state := c.Value

c, err = r.Cookie("verifier")
if err != nil {
http.Error(rw, "verifier cookie not found", http.StatusBadRequest)
return
}
codeVerifier := c.Value

_ = r.ParseForm()
Expand Down Expand Up @@ -152,7 +156,7 @@ func (oa *OIDC) OAuth2Callback(rw http.ResponseWriter, r *http.Request) {
}
}

if len(claims.Profile.Client.Roles) == 0 {
if len(roles) == 0 {
roles = append(roles, schema.GetRoleString(schema.RoleUser))
}

Expand All @@ -163,6 +167,11 @@ func (oa *OIDC) OAuth2Callback(rw http.ResponseWriter, r *http.Request) {
Projects: projects,
AuthSource: schema.AuthViaOIDC,
}

if config.Keys.OpenIDConfig.SyncUserOnLogin {
persistUser(user)
}

oa.authentication.SaveSession(rw, r, user)
log.Infof("login successfull: user: %#v (roles: %v, projects: %v)", user.Username, user.Roles, user.Projects)
ctx := context.WithValue(r.Context(), repository.ContextUserKey, user)
Expand Down
13 changes: 8 additions & 5 deletions pkg/schema/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,11 @@ type LdapConfig struct {
SyncUserOnLogin bool `json:"syncUserOnLogin"`
}

type OpenIDConfig struct {
Provider string `json:"provider"`
SyncUserOnLogin bool `json:"syncUserOnLogin"`
}

type JWTAuthConfig struct {
// Specifies for how long a JWT token shall be valid
// as a string parsable by time.ParseDuration().
Expand Down Expand Up @@ -109,11 +114,9 @@ type ProgramConfig struct {
Validate bool `json:"validate"`

// For LDAP Authentication and user synchronisation.
LdapConfig *LdapConfig `json:"ldap"`
JwtConfig *JWTAuthConfig `json:"jwts"`

// Enable OpenID connect Authentication
OpenIDProvider string `json:"openIDProvider"`
LdapConfig *LdapConfig `json:"ldap"`
JwtConfig *JWTAuthConfig `json:"jwts"`
OpenIDConfig *OpenIDConfig `json:"oidc"`

// If 0 or empty, the session does not expire!
SessionMaxAge string `json:"session-max-age"`
Expand Down

0 comments on commit 6828c97

Please sign in to comment.