Compare commits

..

1 commit

Author SHA1 Message Date
ab542c89f4 Allow using config file as well as env variables
All checks were successful
ci/woodpecker/push/test Pipeline was successful
ci/woodpecker/push/deploy Pipeline was successful
2024-05-28 21:47:51 +02:00
16 changed files with 12 additions and 262 deletions

View file

@ -90,9 +90,8 @@ func init() {
appCmd.AddCommand(appAddCmd)
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, "client-id", "", "", "OpenIDConnect client secret")
appAddCmd.Flags().StringVarP(&appClientSecret, "client-secret", "", "", "OpenIDConnect client secret")
appAddCmd.Flags().StringVarP(&appClientID, "id", "i", "", "ID to identify the app in the storage")
appAddCmd.Flags().StringVarP(&appClientSecret, "secret", "s", "", "OpenIDConnect client secret")
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")

View file

@ -1,65 +0,0 @@
package db
import (
"errors"
"fmt"
"os/exec"
"syscall"
"git.faercol.me/faercol/polyculeconnect/polyculeconnect/cmd/utils"
"git.faercol.me/faercol/polyculeconnect/polyculeconnect/config"
"github.com/spf13/cobra"
)
// connectCmd represents the db connect command
var connectCmd = &cobra.Command{
Use: "connect",
Short: "Connect to the database",
Long: `Connect to the database.`,
Run: func(cmd *cobra.Command, args []string) {
conf := utils.InitConfig("")
if err := connectToDB(conf); err != nil {
utils.Failf("Failed to connect to DB: %s", err.Error())
}
},
}
func connectSQLite(conf *config.StorageConfig) error {
path, err := exec.LookPath("sqlite3")
if err != nil {
if errors.Is(err, exec.ErrNotFound) {
return errors.New("sqlite3 not installed")
}
return fmt.Errorf("failed to find sqlite3 executable: %w", err)
}
if err := syscall.Exec(path, []string{path, conf.File}, nil); err != nil {
return fmt.Errorf("failed to run sqlite3 command: %w", err)
}
return nil
}
func connectToDB(conf *config.AppConfig) error {
switch conf.StorageType {
case string(config.Memory):
return errors.New("no DB associated with memory storage")
case string(config.SQLite):
return connectSQLite(conf.StorageConfig)
default:
return fmt.Errorf("unsupported storage type %q", conf.StorageType)
}
}
func init() {
dbCmd.AddCommand(connectCmd)
// Here you will define your flags and configuration settings.
// Cobra supports Persistent Flags which will work for this command
// and all subcommands, e.g.:
// dbCmd.PersistentFlags().String("foo", "", "A help for foo")
// Cobra supports local flags which will only run when this command
// is called directly, e.g.:
// dbCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
}

View file

@ -1,27 +0,0 @@
package db
import (
"git.faercol.me/faercol/polyculeconnect/polyculeconnect/cmd"
"github.com/spf13/cobra"
)
// dbCmd represents the db command
var dbCmd = &cobra.Command{
Use: "db",
Short: "Manage the database",
Long: `Manage the database.`,
}
func init() {
cmd.RootCmd.AddCommand(dbCmd)
// Here you will define your flags and configuration settings.
// Cobra supports Persistent Flags which will work for this command
// and all subcommands, e.g.:
// dbCmd.PersistentFlags().String("foo", "", "A help for foo")
// Cobra supports local flags which will only run when this command
// is called directly, e.g.:
// dbCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
}

View file

@ -1,60 +0,0 @@
package db
import (
"errors"
"fmt"
"os"
"git.faercol.me/faercol/polyculeconnect/polyculeconnect/cmd/utils"
"git.faercol.me/faercol/polyculeconnect/polyculeconnect/config"
"github.com/spf13/cobra"
)
// destroyCmd represents the db destroy command
var destroyCmd = &cobra.Command{
Use: "destroy",
Short: "Completely delete the current database",
Long: `Delete the current database.`,
Run: func(cmd *cobra.Command, args []string) {
conf := utils.InitConfig("")
if err := deleteDB(conf); err != nil {
utils.Failf("Failed to connect to DB: %s", err.Error())
}
fmt.Println("DB deleted")
},
}
func deleteSqliteDB(path string) error {
if err := os.Remove(path); err != nil {
if errors.Is(err, os.ErrNotExist) { // if the file has already been deleted we don't want to fail here
return nil
}
return fmt.Errorf("failed to delete SQLite file: %w", err)
}
return nil
}
func deleteDB(conf *config.AppConfig) error {
switch conf.StorageType {
case string(config.Memory):
return errors.New("no DB to delete in memory mode")
case string(config.SQLite):
return deleteSqliteDB(conf.StorageConfig.File)
default:
return fmt.Errorf("unsupported storage type %q", conf.StorageType)
}
}
func init() {
dbCmd.AddCommand(destroyCmd)
// Here you will define your flags and configuration settings.
// Cobra supports Persistent Flags which will work for this command
// and all subcommands, e.g.:
// dbCmd.PersistentFlags().String("foo", "", "A help for foo")
// Cobra supports local flags which will only run when this command
// is called directly, e.g.:
// dbCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
}

View file

@ -40,7 +40,7 @@ const (
defaultServerSocket = ""
defaultServerStaticDir = "./"
defaultIssuer = "http://localhost:5000"
defaultIssuer = "locahost"
defaultStorageType = Memory
defaultStorageFile = "./polyculeconnect.db"

View file

@ -4,7 +4,6 @@ import (
"git.faercol.me/faercol/polyculeconnect/polyculeconnect/cmd"
_ "git.faercol.me/faercol/polyculeconnect/polyculeconnect/cmd/app"
_ "git.faercol.me/faercol/polyculeconnect/polyculeconnect/cmd/backend"
_ "git.faercol.me/faercol/polyculeconnect/polyculeconnect/cmd/db"
_ "git.faercol.me/faercol/polyculeconnect/polyculeconnect/cmd/serve"
)

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";
const connectorIDParam = "connector_id";
let connectorForm = document.getElementById("connectorform");
const urlParams = new URLSearchParams(window.location.search);
connectorForm.addEventListener("submit", (e) => {
e.preventDefault();
function chooseConnector(connectorName) {
let nextURL = new URL(window.location.href);
nextURL.searchParams.append(connectorIDParam, connectorName);
setState(STATE_IN_PROGRESS);
let connectorName = document.getElementById("cname").value;
nextURL.searchParams.append("connector_id", connectorName)
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);
}
.form-checkbox {
cursor: pointer;
}
.form-checkbox-label {
color: var(--subtext-0);
font-size: small;
}
.button {
border: none;
color: var(--mantle);

View file

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

View file

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

View file

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

View file

@ -1,6 +1,6 @@
{{ 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-content">
@ -12,10 +12,6 @@
<div class="form-elements">
<input type="text" id="cname" name="connector_id" placeholder="Service name" required
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 class="form-buttons">
<input type="submit" class="button button-accept" value="Continue">