Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: [Issue 582] Log successful and unsuccessful sign in attempts #588

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions samlidp/session.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,13 @@ func (s *Server) GetSession(w http.ResponseWriter, r *http.Request, req *saml.Id
if r.Method == "POST" && r.PostForm.Get("user") != "" {
user := User{}
if err := s.Store.Get(fmt.Sprintf("/users/%s", r.PostForm.Get("user")), &user); err != nil {
s.logger.Printf("ERROR: User '%s' doesn't exists", r.PostForm.Get("user"))
s.sendLoginForm(w, r, req, "Invalid username or password")
return nil
}

if err := bcrypt.CompareHashAndPassword(user.HashedPassword, []byte(r.PostForm.Get("password"))); err != nil {
s.logger.Printf("ERROR: Invalid password for user '%s'", r.PostForm.Get("user"))
s.sendLoginForm(w, r, req, "Invalid username or password")
return nil
}
Expand Down Expand Up @@ -75,6 +77,8 @@ func (s *Server) GetSession(w http.ResponseWriter, r *http.Request, req *saml.Id
Secure: r.URL.Scheme == "https",
Path: "/",
})

s.logger.Printf("User '%s' authenticated successfully", r.PostForm.Get("user"))
return session
}

Expand Down
23 changes: 23 additions & 0 deletions samlidp/session_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,4 +63,27 @@ func TestSessionsCrud(t *testing.T) {
assert.Check(t, is.Equal("{\"sessions\":[]}\n",
w.Body.String()))

// user doesn't exists case
w = httptest.NewRecorder()
r, _ = http.NewRequest("POST", "https://idp.example.com/login",
strings.NewReader("user=unknown&password=dummypassword"))
r.Header.Set("Content-type", "application/x-www-form-urlencoded")
test.Server.ServeHTTP(w, r)
assert.Check(t, is.Equal(http.StatusOK, w.Code))
assert.Check(t, is.Equal("text/html; charset=utf-8",
w.Header().Get("Content-type")))
assert.Check(t, is.Equal(`<html><p>Invalid username or password</p><form method="post" action="https://idp.example.com/sso"><input type="text" name="user" placeholder="user" value="" /><input type="password" name="password" placeholder="password" value="" /><input type="hidden" name="SAMLRequest" value="" /><input type="hidden" name="RelayState" value="" /><input type="submit" value="Log In" /></form></html>`,
w.Body.String()))

// invalid username/password exists case
w = httptest.NewRecorder()
r, _ = http.NewRequest("POST", "https://idp.example.com/login",
strings.NewReader("user=alice&password=dummypassword"))
r.Header.Set("Content-type", "application/x-www-form-urlencoded")
test.Server.ServeHTTP(w, r)
assert.Check(t, is.Equal(http.StatusOK, w.Code))
assert.Check(t, is.Equal("text/html; charset=utf-8",
w.Header().Get("Content-type")))
assert.Check(t, is.Equal(`<html><p>Invalid username or password</p><form method="post" action="https://idp.example.com/sso"><input type="text" name="user" placeholder="user" value="" /><input type="password" name="password" placeholder="password" value="" /><input type="hidden" name="SAMLRequest" value="" /><input type="hidden" name="RelayState" value="" /><input type="submit" value="Log In" /></form></html>`,
w.Body.String()))
}
Loading