Compare commits

..

1 commit

Author SHA1 Message Date
b11897e439 Feat #45: Add CLI commands to manage the DB
All checks were successful
ci/woodpecker/push/test Pipeline was successful
ci/woodpecker/push/deploy Pipeline was successful
2024-03-18 16:40:47 +01:00
17 changed files with 28 additions and 140 deletions

View file

@ -1,7 +1,6 @@
# PolyculeConnect # PolyculeConnect
[![status-badge](https://ci-polycule-connect.chapoline.me/api/badges/1/status.svg)](https://ci-polycule-connect.chapoline.me/repos/1) [![status-badge](https://ci-polycule-connect.chapoline.me/api/badges/1/status.svg)](https://ci-polycule-connect.chapoline.me/repos/1)
[![status-badge](https://ci-server.internal.faercol.me/api/badges/2/status.svg)](https://ci-server.internal.faercol.me/repos/2)
![Project logo](./polyculeconnect/static/img/logo-text.png) ![Project logo](./polyculeconnect/static/img/logo-text.png)

View file

@ -90,9 +90,8 @@ func init() {
appCmd.AddCommand(appAddCmd) appCmd.AddCommand(appAddCmd)
appAddCmd.Flags().StringVarP(&appName, "name", "n", "", "Name to represent the app") appAddCmd.Flags().StringVarP(&appName, "name", "n", "", "Name to represent the app")
appAddCmd.Flags().StringVarP(&appID, "id", "i", "", "ID to identify the app in the storage") appAddCmd.Flags().StringVarP(&appClientID, "id", "i", "", "ID to identify the app in the storage")
appAddCmd.Flags().StringVarP(&appClientID, "client-id", "", "", "OpenIDConnect client secret") appAddCmd.Flags().StringVarP(&appClientSecret, "secret", "s", "", "OpenIDConnect client secret")
appAddCmd.Flags().StringVarP(&appClientSecret, "client-secret", "", "", "OpenIDConnect client secret")
appAddCmd.Flags().StringSliceVarP(&appRedirectURIs, "redirect-uri", "r", []string{}, "Allowed redirect URI") appAddCmd.Flags().StringSliceVarP(&appRedirectURIs, "redirect-uri", "r", []string{}, "Allowed redirect URI")
appAddCmd.Flags().BoolVar(&appInteractive, "interactive", false, "Set the client ID and secret in an interactive way") appAddCmd.Flags().BoolVar(&appInteractive, "interactive", false, "Set the client ID and secret in an interactive way")

View file

@ -43,7 +43,7 @@ func serve() {
logger.L.Infof("Initialized storage backend %q", conf.StorageType) logger.L.Infof("Initialized storage backend %q", conf.StorageType)
dexConf := dex_server.Config{ dexConf := dex_server.Config{
Web: dex_server.WebConfig{ Web: dex_server.WebConfig{
Dir: conf.StaticDir, Dir: "./",
Theme: "default", Theme: "default",
}, },
Storage: storageType, Storage: storageType,

View file

@ -21,7 +21,6 @@ const (
varServerHost envVar = "SERVER_HOST" varServerHost envVar = "SERVER_HOST"
varServerPort envVar = "SERVER_PORT" varServerPort envVar = "SERVER_PORT"
varServerSocket envVar = "SERVER_SOCK_PATH" varServerSocket envVar = "SERVER_SOCK_PATH"
varServerStaticDir envVar = "SERVER_STATIC_DIR"
varIssuer envVar = "ISSUER" varIssuer envVar = "ISSUER"
@ -57,9 +56,8 @@ const (
defaultServerHost = "0.0.0.0" defaultServerHost = "0.0.0.0"
defaultServerPort = 5000 defaultServerPort = 5000
defaultServerSocket = "" defaultServerSocket = ""
defaultServerStaticDir = "./"
defaultIssuer = "http://localhost:5000" defaultIssuer = "locahost"
defaultStorageType = Memory defaultStorageType = Memory
defaultStorageFile = "./polyculeconnect.db" defaultStorageFile = "./polyculeconnect.db"
@ -114,7 +112,6 @@ type AppConfig struct {
StorageType string StorageType string
StorageConfig *StorageConfig StorageConfig *StorageConfig
OpenConnectConfig *OpenConnectConfig OpenConnectConfig *OpenConnectConfig
StaticDir string
} }
func parseLevel(lvlStr string) logrus.Level { func parseLevel(lvlStr string) logrus.Level {
@ -145,7 +142,6 @@ func (ac *AppConfig) getConfFromEnv() {
ac.Host = getStringFromEnv(varServerHost, defaultServerHost) ac.Host = getStringFromEnv(varServerHost, defaultServerHost)
ac.Port = getIntFromEnv(varServerPort, defaultServerPort) ac.Port = getIntFromEnv(varServerPort, defaultServerPort)
ac.SockPath = getStringFromEnv(varServerSocket, defaultServerSocket) ac.SockPath = getStringFromEnv(varServerSocket, defaultServerSocket)
ac.StaticDir = getStringFromEnv(varServerStaticDir, defaultServerStaticDir)
ac.StorageType = getStringFromEnv(varStorageType, string(defaultStorageType)) ac.StorageType = getStringFromEnv(varStorageType, string(defaultStorageType))
ac.StorageConfig.Database = getStringFromEnv(varStorageDB, defaultStorageDB) ac.StorageConfig.Database = getStringFromEnv(varStorageDB, defaultStorageDB)

View file

@ -17,7 +17,6 @@ var defaultConfig = AppConfig{
Port: defaultServerPort, Port: defaultServerPort,
SockPath: defaultServerSocket, SockPath: defaultServerSocket,
StorageType: string(defaultStorageType), StorageType: string(defaultStorageType),
StaticDir: "./",
StorageConfig: &StorageConfig{ StorageConfig: &StorageConfig{
File: defaultStorageFile, File: defaultStorageFile,
Host: defaultStorageHost, Host: defaultStorageHost,

View file

@ -15,31 +15,22 @@ import (
const StaticRoute = "/static/" const StaticRoute = "/static/"
type StaticController struct { type StaticController struct {
baseDir string
}
func NewStaticController(baseDir string) *StaticController {
return &StaticController{
baseDir: baseDir,
}
} }
func (sc *StaticController) ServeHTTP(w http.ResponseWriter, r *http.Request) { func (sc *StaticController) ServeHTTP(w http.ResponseWriter, r *http.Request) {
fs := http.FileServer(http.Dir(sc.baseDir + "/static")) fs := http.FileServer(http.Dir("./static"))
http.StripPrefix(StaticRoute, fs).ServeHTTP(w, r) http.StripPrefix(StaticRoute, fs).ServeHTTP(w, r)
} }
type IndexController struct { type IndexController struct {
l *logrus.Logger l *logrus.Logger
downstreamConstroller http.Handler downstreamConstroller http.Handler
baseDir string
} }
func NewIndexController(l *logrus.Logger, downstream http.Handler, baseDir string) *IndexController { func NewIndexController(l *logrus.Logger, downstream http.Handler) *IndexController {
return &IndexController{ return &IndexController{
l: l, l: l,
downstreamConstroller: downstream, downstreamConstroller: downstream,
baseDir: baseDir,
} }
} }
@ -48,9 +39,9 @@ func (ic IndexController) serveUI(w http.ResponseWriter, r *http.Request) (int,
"issuer": func() string { return "toto" }, "issuer": func() string { return "toto" },
} }
lp := filepath.Join(ic.baseDir, "templates", "index.html") lp := filepath.Join("templates", "index.html")
hdrTpl := filepath.Join(ic.baseDir, "templates", "header.html") hdrTpl := filepath.Join("templates", "header.html")
footTpl := filepath.Join(ic.baseDir, "templates", "footer.html") footTpl := filepath.Join("templates", "footer.html")
tmpl, err := template.New("index.html").Funcs(funcs).ParseFiles(hdrTpl, footTpl, lp) tmpl, err := template.New("index.html").Funcs(funcs).ParseFiles(hdrTpl, footTpl, lp)
if err != nil { if err != nil {
return http.StatusInternalServerError, -1, fmt.Errorf("failed to init template: %w", err) return http.StatusInternalServerError, -1, fmt.Errorf("failed to init template: %w", err)

View file

@ -64,8 +64,8 @@ func New(appConf *config.AppConfig, dexSrv *dex_server.Server, logger *logrus.Lo
} }
controllers := map[string]http.Handler{ controllers := map[string]http.Handler{
ui.StaticRoute: middlewares.WithLogger(ui.NewStaticController(appConf.StaticDir), logger), ui.StaticRoute: middlewares.WithLogger(&ui.StaticController{}, logger),
"/": middlewares.WithLogger(ui.NewIndexController(logger, dexSrv, appConf.StaticDir), logger), "/": middlewares.WithLogger(ui.NewIndexController(logger, dexSrv), logger),
} }
m := http.NewServeMux() m := http.NewServeMux()

View file

@ -1,5 +0,0 @@
let approvalForm = document.getElementById("approvalform");
approvalForm.addEventListener("submit", (e) => {
handleSuccess();
});

View file

@ -1,17 +0,0 @@
const STATE_SUCCESS = "SUCCESS";
const STATE_IN_PROGRESS = "IN PROGRESS"
const STATE_EMPTY = "NO STATE"
const stateKey = "appState"
function getState() {
state = localStorage.getItem(stateKey);
if (state === null) {
return STATE_EMPTY;
}
return state;
}
function setState(value) {
localStorage.setItem(stateKey, value);
}

View file

@ -1,6 +0,0 @@
let backButton = document.getElementById("error-back");
backButton.addEventListener("click", (e) => {
e.preventDefault();
goBackToLogin();
});

View file

@ -1,11 +0,0 @@
let connectorForm = document.getElementById("connectorform");
connectorForm.addEventListener("submit", (e) => {
e.preventDefault();
let connectorName = document.getElementById("cname").value;
let rememberMe = document.getElementById("remember-me").checked;
if (rememberMe === true) {
localStorage.setItem(connectorNameKey, connectorName);
}
chooseConnector(connectorName);
});

View file

@ -1,47 +1,11 @@
const connectorNameKey = "connectorName"; let connectorForm = document.getElementById("connectorform");
const connectorIDParam = "connector_id";
const urlParams = new URLSearchParams(window.location.search); connectorForm.addEventListener("submit", (e) => {
e.preventDefault();
function chooseConnector(connectorName) {
let nextURL = new URL(window.location.href); let nextURL = new URL(window.location.href);
nextURL.searchParams.append(connectorIDParam, connectorName); let connectorName = document.getElementById("cname").value;
setState(STATE_IN_PROGRESS); nextURL.searchParams.append("connector_id", connectorName)
window.location.href = nextURL; window.location.href = nextURL;
} });
// Clean the cache in case previous authentication didn't succeed
// in order not to get stuck in a login loop
function handleFailedState() {
if (getState() !== STATE_SUCCESS) {
localStorage.removeItem(connectorNameKey);
}
}
// Add the connector name to local storage in case the auth succeeded
// and the remember-me box was checked
function handleSuccess(connectorName) {
setState(STATE_SUCCESS);
if (localStorage.getItem(rememberMeKey)) {
localStorage.removeItem(rememberMeKey);
localStorage.setItem(connectorNameKey, connectorName);
}
}
function handleLoginPage() {
handleFailedState();
let connectorName = localStorage.getItem(connectorNameKey);
if (getState() === STATE_SUCCESS && connectorName != null) {
chooseConnector(connectorName);
}
}
function goBackToLogin() {
let nextURL = new URL(window.location.href);
nextURL.searchParams.delete(connectorIDParam);
window.location.href = nextURL;
}
if (window.location.pathname === "/auth" && !urlParams.has(connectorIDParam)) {
handleLoginPage();
}

View file

@ -76,15 +76,6 @@ body {
color: var(--subtext-1); color: var(--subtext-1);
} }
.form-checkbox {
cursor: pointer;
}
.form-checkbox-label {
color: var(--subtext-0);
font-size: small;
}
.button { .button {
border: none; border: none;
color: var(--mantle); color: var(--mantle);

View file

@ -1,7 +1,5 @@
{{ template "header.html" . }} {{ template "header.html" . }}
<script src="/static/scripts/approval.js" defer></script>
<div class="container"> <div class="container">
<div class="container-content"> <div class="container-content">
{{ if .Scopes }} {{ if .Scopes }}
@ -16,7 +14,7 @@
{{ end }} {{ end }}
</div> </div>
<div class="form-buttons" id="approvalform"> <div class="form-buttons">
<form method="post" class="container-form"> <form method="post" class="container-form">
<input type="hidden" name="req" value="{{ .AuthReqID }}" /> <input type="hidden" name="req" value="{{ .AuthReqID }}" />
<input type="hidden" name="approval" value="approve"> <input type="hidden" name="approval" value="approve">

View file

@ -1,10 +1,7 @@
{{ template "header.html" . }} {{ template "header.html" . }}
<script src="/static/scripts/error.js" defer></script>
<div class="container"> <div class="container">
<div class="container-content"> <div class="container-content">
<button id="error-back" class="button button-cancel">Back</button>
<h2>{{ .ErrType }}</h2> <h2>{{ .ErrType }}</h2>
<p>{{ .ErrMsg }}</p> <p>{{ .ErrMsg }}</p>
</div> </div>

View file

@ -16,9 +16,6 @@
<link rel="mask-icon" href="/static/icons/safari-pinned-tab.svg" color="#5bbad5"> <link rel="mask-icon" href="/static/icons/safari-pinned-tab.svg" color="#5bbad5">
<meta name="msapplication-TileColor" content="#9f00a7"> <meta name="msapplication-TileColor" content="#9f00a7">
<meta name="theme-color" content="#ffffff"> <meta name="theme-color" content="#ffffff">
<script src="/static/scripts/appstate.js"></script>
<script src="/static/scripts/index.js"></script>
</head> </head>
<body> <body>

View file

@ -1,6 +1,6 @@
{{ template "header.html" . }} {{ template "header.html" . }}
<script src="/static/scripts/form.js" defer></script> <script src="/static/scripts/index.js" defer></script>
<div class="container"> <div class="container">
<div class="container-content"> <div class="container-content">
@ -12,10 +12,6 @@
<div class="form-elements"> <div class="form-elements">
<input type="text" id="cname" name="connector_id" placeholder="Service name" required <input type="text" id="cname" name="connector_id" placeholder="Service name" required
class="form-input"> class="form-input">
<div>
<input type="checkbox" id="remember-me" name="remember_me" class="form-checkbox">
<label for="remember-me" class="form-checkbox-label">Remember my choice</label>
</div>
</div> </div>
<div class="form-buttons"> <div class="form-buttons">
<input type="submit" class="button button-accept" value="Continue"> <input type="submit" class="button button-accept" value="Continue">