From df08e1ec7245d65b4c165fbb42253a71cddcce3f Mon Sep 17 00:00:00 2001 From: Pierre-Olivier Mercier Date: Sun, 24 Mar 2024 19:19:44 +0100 Subject: [PATCH] admin: Remove hardcoded strings --- admin/api/password.go | 109 ++++++++++++++++++++++++++---------------- 1 file changed, 68 insertions(+), 41 deletions(-) diff --git a/admin/api/password.go b/admin/api/password.go index 759f8eb9..2247e918 100644 --- a/admin/api/password.go +++ b/admin/api/password.go @@ -8,7 +8,9 @@ import ( "net/http" "os" "path" + "strings" "text/template" + "unicode" "srs.epita.fr/fic-server/admin/pki" "srs.epita.fr/fic-server/libfic" @@ -135,7 +137,7 @@ web: http: 0.0.0.0:5556 frontend: issuer: Challenge forensic - logoURL: files/logo/ec2.png + logoURL: {{ .LogoPath }} dir: /srv/dex/web/ oauth2: skipApprovalScreen: true @@ -158,7 +160,7 @@ const dexpasswdtpl = `{{ "{{" }} template "header.html" . {{ "}}" }}

- Bienvenue au challenge Forensic ! + Bienvenue au {{ .Name }} !

@@ -203,54 +205,79 @@ type dexConfigClient struct { } type dexConfig struct { - Issuer string - Clients []dexConfigClient - Teams []*fic.Team + Name string + Issuer string + Clients []dexConfigClient + Teams []*fic.Team + LogoPath string } func genDexConfig() ([]byte, error) { - if teams, err := fic.GetTeams(); err != nil { - return nil, err - } else if OidcSecret == "" { + if OidcSecret == "" { return nil, fmt.Errorf("Unable to generate dex configuration: OIDC Secret not defined. Please define FICOIDC_SECRET in your environment.") - } else { - b := bytes.NewBufferString("") + } - if challengeInfo, err := GetChallengeInfo(); err != nil { - return nil, fmt.Errorf("Cannot create template: %w", err) - } else if dexTmpl, err := template.New("dexcfg").Parse(dexcfgtpl); err != nil { - return nil, fmt.Errorf("Cannot create template: %w", err) - } else if err = dexTmpl.Execute(b, dexConfig{ - Issuer: "https://" + OidcIssuer, - Clients: []dexConfigClient{ - dexConfigClient{ - Id: OidcClientId, - Name: challengeInfo.Title, - RedirectURIs: []string{"https://" + OidcIssuer + "/challenge_access/auth"}, - Secret: OidcSecret, - }, + teams, err := fic.GetTeams() + if err != nil { + return nil, err + } + + b := bytes.NewBufferString("") + + challengeInfo, err := GetChallengeInfo() + if err != nil { + return nil, fmt.Errorf("Cannot create template: %w", err) + } + + // Lower the first letter to be included in a sentence. + name := []rune(challengeInfo.Title) + if len(name) > 0 { + name[0] = unicode.ToLower(name[0]) + } + + logoPath := "" + if len(challengeInfo.MainLogo) > 0 { + logoPath = strings.Replace(challengeInfo.MainLogo[len(challengeInfo.MainLogo)-1], "$FILES$", fic.FilesDir, -1) + } + + dexTmpl, err := template.New("dexcfg").Parse(dexcfgtpl) + if err != nil { + return nil, fmt.Errorf("Cannot create template: %w", err) + } + + err = dexTmpl.Execute(b, dexConfig{ + Name: string(name), + Issuer: "https://" + OidcIssuer, + Clients: []dexConfigClient{ + dexConfigClient{ + Id: OidcClientId, + Name: challengeInfo.Title, + RedirectURIs: []string{"https://" + OidcIssuer + "/challenge_access/auth"}, + Secret: OidcSecret, }, - Teams: teams, - }); err != nil { - return nil, fmt.Errorf("An error occurs during template execution: %w", err) - } else { - // Also generate team associations - for _, team := range teams { - if _, err := os.Stat(path.Join(TeamsDir, fmt.Sprintf("team%02d", team.Id))); err == nil { - if err = os.Remove(path.Join(TeamsDir, fmt.Sprintf("team%02d", team.Id))); err != nil { - log.Println("Unable to remove existing association symlink:", err.Error()) - return nil, fmt.Errorf("Unable to remove existing association symlink: %s", err.Error()) - } - } - if err := os.Symlink(fmt.Sprintf("%d", team.Id), path.Join(TeamsDir, fmt.Sprintf("team%02d", team.Id))); err != nil { - log.Println("Unable to create association symlink:", err.Error()) - return nil, fmt.Errorf("Unable to create association symlink: %s", err.Error()) - } - } + }, + Teams: teams, + LogoPath: logoPath, + }) + if err != nil { + return nil, fmt.Errorf("An error occurs during template execution: %w", err) + } - return b.Bytes(), nil + // Also generate team associations + for _, team := range teams { + if _, err := os.Stat(path.Join(TeamsDir, fmt.Sprintf("team%02d", team.Id))); err == nil { + if err = os.Remove(path.Join(TeamsDir, fmt.Sprintf("team%02d", team.Id))); err != nil { + log.Println("Unable to remove existing association symlink:", err.Error()) + return nil, fmt.Errorf("Unable to remove existing association symlink: %s", err.Error()) + } + } + if err := os.Symlink(fmt.Sprintf("%d", team.Id), path.Join(TeamsDir, fmt.Sprintf("team%02d", team.Id))); err != nil { + log.Println("Unable to create association symlink:", err.Error()) + return nil, fmt.Errorf("Unable to create association symlink: %s", err.Error()) } } + + return b.Bytes(), nil } func genDexPasswordTpl() ([]byte, error) {