Skip to content

Commit f057af4

Browse files
committed
Add option to restrict login (only allow admins)
1 parent 522092a commit f057af4

File tree

2 files changed

+8
-0
lines changed

2 files changed

+8
-0
lines changed

config/config.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ var (
4747
ApiTokenExpirationTime time.Duration = getDuration("API_TOKEN_EXPIRATION_TIME", 3*24*time.Hour)
4848
MinPasswordLength int = getInt("MIN_PASSWORD_LENGTH", 12)
4949
InternalIPs []net.IP = parseIPs(getString("INTERNAL_IPS", ""))
50+
RestrictLoginToAdmins bool = getBool("RESTRICT_LOGIN_TO_ADMINS", false)
5051

5152
UseTestDatabase bool = getBool("USE_TEST_DATABASE", false) // TODO: remove in prod - this function deletes the whole database
5253
)

service/user.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
package service
22

33
import (
4+
"slices"
45
"time"
56

7+
"github.com/ProjectLighthouseCAU/heimdall/config"
68
"github.com/ProjectLighthouseCAU/heimdall/crypto"
79
"github.com/ProjectLighthouseCAU/heimdall/model"
810
"github.com/ProjectLighthouseCAU/heimdall/repository"
@@ -62,6 +64,11 @@ func (s *UserService) Login(username, password string, session *session.Session)
6264
if !crypto.PasswordMatchesHash(password, user.Password) {
6365
return nil, model.UnauthorizedError{Message: "Invalid credentials", Err: nil}
6466
}
67+
if config.RestrictLoginToAdmins {
68+
if slices.ContainsFunc(user.Roles, func(role model.Role) bool { return role.Name == config.AdminRoleName }) {
69+
return nil, model.ForbiddenError{Message: "Login is currently restricted to admins only"}
70+
}
71+
}
6572

6673
session.Set("userid", user.ID)
6774
session.Set("username", user.Username)

0 commit comments

Comments
 (0)