refactor(login): minor fixes
Some checks failed
Dev / build-local (push) Has been cancelled
Dev / migrate-boltdb (push) Has been cancelled
Dev / migrate-mysql (push) Has been cancelled
Dev / migrate-mariadb (push) Has been cancelled
Dev / migrate-postgres (push) Has been cancelled
Dev / integrate-boltdb (push) Has been cancelled
Dev / integrate-mysql (push) Has been cancelled
Dev / integrate-mariadb (push) Has been cancelled
Dev / integrate-postgres (push) Has been cancelled
Dev / deploy-server (push) Has been cancelled
Dev / deploy-runner (push) Has been cancelled

This commit is contained in:
Denis Gukov 2024-12-31 01:14:36 +05:00
parent 1fa412a25a
commit 6443764ad5
No known key found for this signature in database
GPG Key ID: 044381366A5D4731

View File

@ -6,6 +6,7 @@ import (
"crypto/tls"
"encoding/base64"
"encoding/json"
"errors"
"fmt"
"math/rand"
"net/http"
@ -16,13 +17,13 @@ import (
"text/template"
"time"
"github.com/coreos/go-oidc/v3/oidc"
"github.com/go-ldap/ldap/v3"
"github.com/gorilla/mux"
"github.com/semaphoreui/semaphore/api/helpers"
"github.com/semaphoreui/semaphore/db"
"github.com/semaphoreui/semaphore/pkg/random"
"github.com/semaphoreui/semaphore/util"
"github.com/coreos/go-oidc/v3/oidc"
"github.com/go-ldap/ldap/v3"
"github.com/gorilla/mux"
log "github.com/sirupsen/logrus"
"golang.org/x/crypto/bcrypt"
"golang.org/x/oauth2"
@ -202,7 +203,7 @@ func loginByPassword(store db.Store, login string, password string) (user db.Use
func loginByLDAP(store db.Store, ldapUser db.User) (user db.User, err error) {
user, err = store.GetUserByLoginOrEmail(ldapUser.Username, ldapUser.Email)
if err == db.ErrNotFound {
if errors.Is(err, db.ErrNotFound) {
user, err = store.CreateUserWithoutPassword(ldapUser)
}
@ -296,7 +297,7 @@ func login(w http.ResponseWriter, r *http.Request) {
}
if err != nil {
if err == db.ErrNotFound {
if errors.Is(err, db.ErrNotFound) {
w.WriteHeader(http.StatusUnauthorized)
return
}
@ -329,7 +330,7 @@ func logout(w http.ResponseWriter, r *http.Request) {
func getOidcProvider(id string, ctx context.Context, redirectPath string) (*oidc.Provider, *oauth2.Config, error) {
provider, ok := util.Config.OidcProviders[id]
if !ok {
return nil, nil, fmt.Errorf("No such provider: %s", id)
return nil, nil, fmt.Errorf("no such provider: %s", id)
}
config := oidc.ProviderConfig{
IssuerURL: provider.Endpoint.IssuerURL,
@ -392,14 +393,14 @@ func getOidcProvider(id string, ctx context.Context, redirectPath string) (*oidc
Scopes: provider.Scopes,
}
if len(oauthConfig.RedirectURL) == 0 {
rurl, err := url.JoinPath(util.Config.WebHost, "api/auth/oidc", id, "redirect")
redirectURL, err := url.JoinPath(util.Config.WebHost, "api/auth/oidc", id, "redirect")
if err != nil {
return nil, nil, err
}
oauthConfig.RedirectURL = rurl
oauthConfig.RedirectURL = redirectURL
if rurl != redirectPath {
if redirectURL != redirectPath {
oauthConfig.RedirectURL += redirectPath
}
}