server,db: flag for disabling user login

This commit is contained in:
Joe Bowers 2015-09-25 14:24:51 -07:00
parent 825c3cf21b
commit 60a36e2c2e
6 changed files with 129 additions and 25 deletions

View file

@ -0,0 +1,4 @@
-- +migrate Up
ALTER TABLE authd_user ADD COLUMN disabled boolean;
UPDATE authd_user SET "disabled" = FALSE;

File diff suppressed because one or more lines are too long

View file

@ -417,6 +417,7 @@ type userModel struct {
Email string `db:"email"` Email string `db:"email"`
EmailVerified bool `db:"email_verified"` EmailVerified bool `db:"email_verified"`
DisplayName string `db:"display_name"` DisplayName string `db:"display_name"`
Disabled bool `db:"disabled"`
Admin bool `db:"admin"` Admin bool `db:"admin"`
CreatedAt int64 `db:"created_at"` CreatedAt int64 `db:"created_at"`
} }
@ -428,6 +429,7 @@ func (u *userModel) user() (user.User, error) {
Email: u.Email, Email: u.Email,
EmailVerified: u.EmailVerified, EmailVerified: u.EmailVerified,
Admin: u.Admin, Admin: u.Admin,
Disabled: u.Disabled,
} }
if u.CreatedAt != 0 { if u.CreatedAt != 0 {
@ -444,6 +446,7 @@ func newUserModel(u *user.User) (*userModel, error) {
Email: u.Email, Email: u.Email,
EmailVerified: u.EmailVerified, EmailVerified: u.EmailVerified,
Admin: u.Admin, Admin: u.Admin,
Disabled: u.Disabled,
} }
if !u.CreatedAt.IsZero() { if !u.CreatedAt.IsZero() {

View file

@ -326,6 +326,10 @@ func (s *Server) Login(ident oidc.Identity, key string) (string, error) {
return "", err return "", err
} }
if usr.Disabled {
return "", user.ErrorNotFound
}
ses, err = s.SessionManager.AttachUser(sessionID, usr.ID) ses, err = s.SessionManager.AttachUser(sessionID, usr.ID)
if err != nil { if err != nil {
return "", err return "", err

View file

@ -261,6 +261,74 @@ func TestServerLoginUnrecognizedSessionKey(t *testing.T) {
} }
} }
func TestServerLoginDisabledUser(t *testing.T) {
ci := oidc.ClientIdentity{
Credentials: oidc.ClientCredentials{
ID: "XXX",
Secret: "secrete",
},
Metadata: oidc.ClientMetadata{
RedirectURLs: []url.URL{
url.URL{
Scheme: "http",
Host: "client.example.com",
Path: "/callback",
},
},
},
}
ciRepo := client.NewClientIdentityRepo([]oidc.ClientIdentity{ci})
km := &StaticKeyManager{
signer: &StaticSigner{sig: []byte("beer"), err: nil},
}
sm := session.NewSessionManager(session.NewSessionRepo(), session.NewSessionKeyRepo())
sm.GenerateCode = staticGenerateCodeFunc("fakecode")
sessionID, err := sm.NewSession("test_connector_id", ci.Credentials.ID, "bogus", ci.Metadata.RedirectURLs[0], "", false, []string{"openid"})
if err != nil {
t.Fatalf("Unexpected error: %v", err)
}
userRepo, err := makeNewUserRepo()
if err != nil {
t.Fatalf("Unexpected error: %v", err)
}
err = userRepo.Create(nil, user.User{
ID: "disabled-1",
Email: "disabled@example.com",
Disabled: true,
})
if err != nil {
t.Fatalf("Unexpected error: %v", err)
}
err = userRepo.AddRemoteIdentity(nil, "disabled-1", user.RemoteIdentity{
ConnectorID: "test_connector_id",
ID: "disabled-connector-id",
})
srv := &Server{
IssuerURL: url.URL{Scheme: "http", Host: "server.example.com"},
KeyManager: km,
SessionManager: sm,
ClientIdentityRepo: ciRepo,
UserRepo: userRepo,
}
ident := oidc.Identity{ID: "disabled-connector-id", Name: "elroy", Email: "elroy@example.com"}
key, err := sm.NewSessionKey(sessionID)
if err != nil {
t.Fatalf("Unexpected error: %v", err)
}
_, err = srv.Login(ident, key)
if err == nil {
t.Errorf("disabled user was allowed to log in")
}
}
func TestServerCodeToken(t *testing.T) { func TestServerCodeToken(t *testing.T) {
ci := oidc.ClientIdentity{ ci := oidc.ClientIdentity{
Credentials: oidc.ClientCredentials{ Credentials: oidc.ClientCredentials{

View file

@ -41,6 +41,8 @@ type User struct {
Admin bool Admin bool
Disabled bool
CreatedAt time.Time CreatedAt time.Time
} }