Commit 4d8466cb authored by Sietse Ringers's avatar Sietse Ringers
Browse files

Determine default schemes path using XDG

parent 9cba60f4
...@@ -66,8 +66,9 @@ func (s *Server) verifyConfiguration(configuration *server.Configuration) error ...@@ -66,8 +66,9 @@ func (s *Server) verifyConfiguration(configuration *server.Configuration) error
return server.LogError(err) return server.LogError(err)
} }
if !exists { if !exists {
return server.LogError(errors.New("Nonexisting schemes_path provided")) return server.LogError(errors.Errorf("Nonexisting schemes_path provided: %s", s.conf.SchemesPath))
} }
s.conf.Logger.WithField("schemes_path", s.conf.SchemesPath).Info("Determined schemes path")
if s.conf.SchemesAssetsPath == "" { if s.conf.SchemesAssetsPath == "" {
s.conf.IrmaConfiguration, err = irma.NewConfiguration(s.conf.SchemesPath) s.conf.IrmaConfiguration, err = irma.NewConfiguration(s.conf.SchemesPath)
} else { } else {
......
...@@ -10,6 +10,7 @@ import ( ...@@ -10,6 +10,7 @@ import (
"reflect" "reflect"
"runtime" "runtime"
"runtime/debug" "runtime/debug"
"strings"
"github.com/go-errors/errors" "github.com/go-errors/errors"
"github.com/privacybydesign/gabi" "github.com/privacybydesign/gabi"
...@@ -45,7 +46,7 @@ type Configuration struct { ...@@ -45,7 +46,7 @@ type Configuration struct {
// (Optional) email address of server admin, for incidental notifications such as breaking API changes // (Optional) email address of server admin, for incidental notifications such as breaking API changes
// See https://github.com/privacybydesign/irmago/tree/master/server#specifying-an-email-address // See https://github.com/privacybydesign/irmago/tree/master/server#specifying-an-email-address
// for more information // for more information
Email string Email string `json:"email" mapstructure:"email"`
} }
// SessionResult contains session information such as the session status, type, possible errors, // SessionResult contains session information such as the session status, type, possible errors,
...@@ -250,16 +251,47 @@ func LocalIP() (string, error) { ...@@ -250,16 +251,47 @@ func LocalIP() (string, error) {
return "", errors.New("No IP found") return "", errors.New("No IP found")
} }
// DefaultSchemesPath returns the default path for IRMA schemes, using XDG Base Directory Specification
// https://specifications.freedesktop.org/basedir-spec/basedir-spec-latest.html:
// - %LOCALAPPDATA% (i.e. C:\Users\$user\AppData\Local) if on Windows,
// - $XDG_DATA_HOME if set, otherwise $HOME/.local/share
// - $XDG_DATA_DIRS if set, otherwise /usr/local/share/ and /usr/share/
// - then the OSes temp dir (os.TempDir()),
// returning the first of these that exists or can be created.
func DefaultSchemesPath() string { func DefaultSchemesPath() string {
candidates := make([]string, 0, 2) candidates := make([]string, 0, 8)
if runtime.GOOS != "windows" { home := os.Getenv("HOME")
candidates = append(candidates, "/var/tmp/irma/irma_configuration") xdgDataHome := os.Getenv("XDG_DATA_HOME")
xdgDataDirs := os.Getenv("XDG_DATA_DIRS")
if runtime.GOOS == "windows" {
appdata := os.Getenv("LOCALAPPDATA") // C:\Users\$user\AppData\Local
if appdata != "" {
candidates = append(candidates, appdata)
}
}
if xdgDataHome != "" {
candidates = append(candidates, xdgDataHome)
}
if xdgDataHome == "" && home != "" {
candidates = append(candidates, filepath.Join(home, ".local", "share"))
} }
candidates = append(candidates, filepath.Join(os.TempDir(), "irma", "irma_configuration")) if xdgDataDirs != "" {
return firstWritablePath(candidates) candidates = append(candidates, strings.Split(xdgDataDirs, ":")...)
} else {
candidates = append(candidates, "/usr/local/share", "/usr/share")
}
candidates = append(candidates, filepath.Join(os.TempDir()))
for i := range candidates {
candidates[i] = filepath.Join(candidates[i], "irma", "irma_configuration")
}
return firstExistingPath(candidates)
} }
func firstWritablePath(paths []string) string { func firstExistingPath(paths []string) string {
for _, path := range paths { for _, path := range paths {
if err := fs.EnsureDirectoryExists(path); err != nil { if err := fs.EnsureDirectoryExists(path); err != nil {
continue continue
......
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