Commit a4a40039 authored by Nicolas Pernoud's avatar Nicolas Pernoud
Browse files

feat: allow setting up of in memory tokens lifetime with env variable

parent f23970aa
Pipeline #8088 passed with stages
in 2 minutes and 53 seconds
......@@ -24,7 +24,8 @@
"ADMIN_ROLE": "ADMINS",
"HOSTNAME": "vestibule.127.0.0.1.nip.io",
"ONLYOFFICE_TITLE": "VestibuleOffice",
"ONLYOFFICE_SERVER": "https://localhost:2443"
"ONLYOFFICE_SERVER": "https://localhost:2443",
"INMEMORY_TOKEN_LIFE_DAYS": "2"
},
"args": ["-debug", "-https_port=1443"],
"showLog": true
......
......@@ -5,6 +5,7 @@ import (
"errors"
"fmt"
"net/http"
"os"
"sort"
"strconv"
"strings"
......@@ -18,9 +19,23 @@ import (
var (
//UsersFile is the file containing the users
UsersFile = "./configs/users.json"
UsersFile = "./configs/users.json"
tokenLifetime time.Duration
)
func setTokenLifetime() time.Duration {
days := 1
i, err := strconv.Atoi(os.Getenv("INMEMORY_TOKEN_LIFE_DAYS"))
if err == nil && i >= 1 && i <= 10000 {
days = i
}
return time.Duration(days*24) * time.Hour
}
func init() {
tokenLifetime = setTokenLifetime()
}
// HandleInMemoryLogin validate the username and password provided in the function body against a local file and return a token if the user is found
func (m Manager) HandleInMemoryLogin(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodPost {
......@@ -49,7 +64,7 @@ func (m Manager) HandleInMemoryLogin(w http.ResponseWriter, r *http.Request) {
return
}
tokenData := TokenData{User: User{ID: user.ID, Login: user.Login, Email: user.Email, Roles: user.Roles}, XSRFToken: xsrfToken}
tokens.Manager.StoreData(tokenData, m.Hostname, authTokenKey, 24*time.Hour, w)
tokens.Manager.StoreData(tokenData, m.Hostname, authTokenKey, tokenLifetime, w)
// Log the connexion
log.Logger.Printf("| %v (%v %v) | Login success | %v | %v", user.Login, user.Name, user.Surname, r.RemoteAddr, log.GetCityAndCountryFromRequest(r))
}
......
package auth
import (
"os"
"testing"
"time"
)
func Test_setTokenLifetime(t *testing.T) {
type args struct {
key string
value string
}
tests := []struct {
name string
args args
want time.Duration
}{
{"no environnement", args{"OTHER_ENV", "10"}, 24 * time.Hour},
{"wrong type", args{"INMEMORY_TOKEN_LIFE_DAYS", "A_STRING"}, 24 * time.Hour},
{"to small", args{"INMEMORY_TOKEN_LIFE_DAYS", "-1"}, 24 * time.Hour},
{"to big", args{"INMEMORY_TOKEN_LIFE_DAYS", "11 000"}, 24 * time.Hour},
{"ok", args{"INMEMORY_TOKEN_LIFE_DAYS", "3"}, 72 * time.Hour},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
os.Setenv(tt.args.key, tt.args.value)
if got := setTokenLifetime(); got != tt.want {
t.Errorf("setTokenLifetime() = %v, want %v", got, tt.want)
}
})
}
}
......@@ -32,7 +32,7 @@
<div class="navbar-brand">
<div class="navbar-item">
<a class="button is-primary is-rounded is-outlined" href="https://www.github.com/nicolaspernoud/Vestibule" target="_blank" rel="noopener noreferrer">
<span>4.3.53</span>
<span>4.3.54</span>
<span class="icon">
<svg
class="svg-inline--fa fa-github fa-w-16"
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment