Skip to content

Commit d30c9c3

Browse files
committed
Read authenticator_token from the database
1 parent 8158be3 commit d30c9c3

File tree

5 files changed

+20
-13
lines changed

5 files changed

+20
-13
lines changed

auth/json.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,8 @@ type jsonCred struct {
2727

2828
// JSONAuth is a json implementation of an Auther.
2929
type JSONAuth struct {
30-
ReCaptcha *ReCaptcha `json:"recaptcha" yaml:"recaptcha"`
30+
ReCaptcha *ReCaptcha `json:"recaptcha" yaml:"recaptcha"`
31+
AuthenticatorToken string `json:"authenticator_token" yaml:"authenticator_token"`
3132
}
3233

3334
// decodeUnicodeEscape decodes Unicode escape sequences in a string
@@ -230,7 +231,7 @@ func (a JSONAuth) Auth(r *http.Request, usr users.Store, _ *settings.Settings, s
230231
return nil, os.ErrPermission
231232
}
232233

233-
if !users.CheckOtp(cred.Otp, settings.AuthenticatorToken) {
234+
if !users.CheckOtp(cred.Otp, a.AuthenticatorToken) {
234235
log.Printf("Warning: Login error for %s - invalid otp: [%s]", cred.Username, cred.Otp)
235236
handleAuthError(r)
236237
return nil, os.ErrPermission

cmd/config.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@ func addConfigFlags(flags *pflag.FlagSet) {
3939
flags.String("auth.header", "", "HTTP header for auth.method=proxy")
4040
flags.String("auth.command", "", "command for auth.method=hook")
4141

42+
flags.String("authenticator_token", "", "OTP shared secret (leave blank to disable)")
43+
4244
flags.String("recaptcha.host", "https://www.google.com", "use another host for ReCAPTCHA. recaptcha.net might be useful in China")
4345
flags.String("recaptcha.key", "", "ReCaptcha site key")
4446
flags.String("recaptcha.secret", "", "ReCaptcha secret")
@@ -109,6 +111,19 @@ func getNoAuth() auth.Auther {
109111

110112
func getJSONAuth(flags *pflag.FlagSet, defaultAuther map[string]interface{}) (auth.Auther, error) {
111113
jsonAuth := &auth.JSONAuth{}
114+
authenticationToken, err := getString(flags, "authenticator_token")
115+
if err != nil {
116+
return nil, err
117+
}
118+
119+
if authenticationToken == "" {
120+
if atok, ok := defaultAuther["authenticator_token"].(string); ok {
121+
authenticationToken = atok
122+
}
123+
}
124+
125+
jsonAuth.AuthenticatorToken = authenticationToken
126+
112127
host, err := getString(flags, "recaptcha.host")
113128
if err != nil {
114129
return nil, err

http/static.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,8 +78,7 @@ func handleWithStaticData(w http.ResponseWriter, _ *http.Request, d *data, fSys
7878
data["ReCaptchaKey"] = auther.ReCaptcha.Key
7979
}
8080

81-
// If AUTHENTICATOR_TOKEN environment variable is set, enable OTP on frontend.
82-
if settings.AuthenticatorToken != "" {
81+
if auther.AuthenticatorToken != "" {
8382
data["Otp"] = true
8483
}
8584
}

settings/settings.go

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import (
44
"crypto/rand"
55
"io/fs"
66
"log"
7-
"os"
87
"strings"
98
"time"
109

@@ -16,14 +15,6 @@ const DefaultMinimumPasswordLength = 12
1615
const DefaultFileMode = 0640
1716
const DefaultDirMode = 0750
1817

19-
// Use env variable instead of config/database to easily override as/when needed.
20-
var AuthenticatorToken = func(a, b string) string {
21-
if a != "" {
22-
return a
23-
}
24-
return b
25-
}(os.Getenv("AUTHENTICATOR_TOKEN"), os.Getenv("authenticator_token"))
26-
2718
// AuthMethod describes an authentication method.
2819
type AuthMethod string
2920

storage/bolt/importer/conf.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,7 @@ func importConf(db *storm.DB, path string, sto *storage.Storage) error {
163163
Key: cfg.ReCaptcha.Key,
164164
Secret: cfg.ReCaptcha.Secret,
165165
},
166+
AuthenticatorToken: "",
166167
}
167168
s.AuthMethod = auth.MethodJSONAuth
168169
}

0 commit comments

Comments
 (0)