@@ -24,10 +24,17 @@ import (
2424const sessionCookie = "gowdk_flagship_session"
2525
2626func Login (_ context.Context , values form.Values ) (response.Response , error ) {
27+ if len (sessionSecret ()) == 0 {
28+ return response .RedirectTo ("/?login=failed" ), nil
29+ }
30+ wantEmail , wantPassword , ok := configuredCredentials ()
31+ if ! ok {
32+ return response .RedirectTo ("/?login=failed" ), nil
33+ }
34+
2735 email := strings .TrimSpace (values .First ("email" ))
2836 password := values .First ("password" )
29- if ! constantEqual (email , env ("GOWDK_FLAGSHIP_EMAIL" , "demo@example.com" )) ||
30- ! constantEqual (password , env ("GOWDK_FLAGSHIP_PASSWORD" , "demo-password" )) {
37+ if ! constantEqual (email , wantEmail ) || ! constantEqual (password , wantPassword ) {
3138 return response .RedirectTo ("/?login=failed" ), nil
3239 }
3340
@@ -127,6 +134,9 @@ var sessions = struct {
127134}{Values : map [string ]session {}}
128135
129136func currentSession (request * http.Request ) (session , bool ) {
137+ if len (sessionSecret ()) == 0 {
138+ return session {}, false
139+ }
130140 if request == nil {
131141 return session {}, false
132142 }
@@ -159,11 +169,21 @@ func sign(value string) string {
159169}
160170
161171func signature (value string ) string {
162- mac := hmac .New (sha256 .New , [] byte ( env ( "GOWDK_FLAGSHIP_SECRET" , "development-flagship-secret-change-me" ) ))
172+ mac := hmac .New (sha256 .New , sessionSecret ( ))
163173 _ , _ = mac .Write ([]byte (value ))
164174 return base64 .RawURLEncoding .EncodeToString (mac .Sum (nil ))
165175}
166176
177+ func sessionSecret () []byte {
178+ return []byte (strings .TrimSpace (os .Getenv ("GOWDK_FLAGSHIP_SECRET" )))
179+ }
180+
181+ func configuredCredentials () (email , password string , ok bool ) {
182+ email = env ("GOWDK_FLAGSHIP_EMAIL" , "demo@example.com" )
183+ password = strings .TrimSpace (os .Getenv ("GOWDK_FLAGSHIP_PASSWORD" ))
184+ return email , password , password != ""
185+ }
186+
167187func sessionDuration () time.Duration {
168188 return 12 * time .Hour
169189}
0 commit comments