#3, more info for own authentication handler

This commit is contained in:
Magnus Åhall 2024-01-30 07:06:42 +01:00
parent 772c54f6fe
commit 99ce47e9e5
2 changed files with 34 additions and 22 deletions

View File

@ -214,7 +214,18 @@ func (db *T) RetrieveSession(uuid string) (sess *session.T, err error) {// {{{
return return
}// }}} }// }}}
func (db *T) SetSessionUser(uuid string, userID int) (err error) { // {{{ func (db *T) SetSessionUser(uuid string, userID int) (err error) { // {{{
_, err = db.Conn.Exec("UPDATE _webservice.session SET user_id=$1 WHERE uuid=$2", userID, uuid) _, err = db.Conn.Exec(`
UPDATE _webservice.session
SET
user_id = CASE
WHEN $1 <= 0 THEN NULL
ELSE $1
END
WHERE uuid=$2
`,
userID,
uuid,
)
if err != nil { if err != nil {
return return
} }

View File

@ -15,8 +15,10 @@ import (
) )
type AuthenticationRequest struct { type AuthenticationRequest struct {
UserID int `json:"-"`
Username string Username string
Password string Password string
Additional interface{}
} }
type AuthenticationResponse struct { type AuthenticationResponse struct {
@ -75,7 +77,7 @@ func (service *Service) sessionNew(w http.ResponseWriter, r *http.Request, foo *
w.Write(respJSON) w.Write(respJSON)
} // }}} } // }}}
func (service *Service) sessionAuthenticate(w http.ResponseWriter, r *http.Request, sess *session.T) { // {{{ func (service *Service) sessionAuthenticate(w http.ResponseWriter, r *http.Request, sess *session.T) { // {{{
var authenticated bool var authenticatedByFramework bool
var authResponse AuthenticationResponse var authResponse AuthenticationResponse
var err error var err error
reqBody, _ := io.ReadAll(r.Body) reqBody, _ := io.ReadAll(r.Body)
@ -91,34 +93,33 @@ func (service *Service) sessionAuthenticate(w http.ResponseWriter, r *http.Reque
// Authenticate against webservice user table if using a database. // Authenticate against webservice user table if using a database.
var userID int var userID int
if service.Db != nil { if service.Db != nil {
authenticated, userID, err = service.Db.Authenticate(authRequest.Username, authRequest.Password) authenticatedByFramework, userID, err = service.Db.Authenticate(authRequest.Username, authRequest.Password)
if err != nil { if err != nil {
service.errorHandler(err, "001-A002", w) service.errorHandler(err, "001-A002", w)
return return
} }
authRequest.UserID = userID
if authenticated && userID > 0 {
err = service.Db.SetSessionUser(sess.UUID, userID)
if err != nil {
service.errorHandler(err, "001-A003", w)
return
}
}
} }
// The authentication handler is provided with the authenticated response of the possible database authentication, // The authentication handler is provided with the authenticated response of the possible database authentication,
// and given a chance to override it. // and given a chance to override it.
authResponse, err = service.authenticationHandler(authRequest, authenticated) authResponse, err = service.authenticationHandler(authRequest, authenticatedByFramework)
if err != nil { if err != nil {
service.errorHandler(err, "001-F002", w) service.errorHandler(err, "001-F002", w)
return return
} }
authResponse.UserID = userID authResponse.UserID = userID
authResponse.OK = true authResponse.OK = true
sess.Authenticated = authResponse.Authenticated sess.Authenticated = authResponse.Authenticated
if authResponse.Authenticated && userID > 0 {
err = service.Db.SetSessionUser(sess.UUID, userID)
if err != nil {
service.errorHandler(err, "001-A003", w)
return
}
}
authResp, _ := json.Marshal(authResponse) authResp, _ := json.Marshal(authResponse)
w.Write(authResp) w.Write(authResp)
} // }}} } // }}}