#3, more info for own authentication handler
This commit is contained in:
parent
772c54f6fe
commit
99ce47e9e5
@ -214,7 +214,18 @@ func (db *T) RetrieveSession(uuid string) (sess *session.T, err error) {// {{{
|
||||
return
|
||||
}// }}}
|
||||
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 {
|
||||
return
|
||||
}
|
||||
|
27
session.go
27
session.go
@ -15,8 +15,10 @@ import (
|
||||
)
|
||||
|
||||
type AuthenticationRequest struct {
|
||||
UserID int `json:"-"`
|
||||
Username string
|
||||
Password string
|
||||
Additional interface{}
|
||||
}
|
||||
|
||||
type AuthenticationResponse struct {
|
||||
@ -75,7 +77,7 @@ func (service *Service) sessionNew(w http.ResponseWriter, r *http.Request, foo *
|
||||
w.Write(respJSON)
|
||||
} // }}}
|
||||
func (service *Service) sessionAuthenticate(w http.ResponseWriter, r *http.Request, sess *session.T) { // {{{
|
||||
var authenticated bool
|
||||
var authenticatedByFramework bool
|
||||
var authResponse AuthenticationResponse
|
||||
var err error
|
||||
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.
|
||||
var userID int
|
||||
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 {
|
||||
service.errorHandler(err, "001-A002", w)
|
||||
return
|
||||
}
|
||||
|
||||
if authenticated && userID > 0 {
|
||||
err = service.Db.SetSessionUser(sess.UUID, userID)
|
||||
if err != nil {
|
||||
service.errorHandler(err, "001-A003", w)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
authRequest.UserID = userID
|
||||
}
|
||||
|
||||
// The authentication handler is provided with the authenticated response of the possible database authentication,
|
||||
// and given a chance to override it.
|
||||
authResponse, err = service.authenticationHandler(authRequest, authenticated)
|
||||
authResponse, err = service.authenticationHandler(authRequest, authenticatedByFramework)
|
||||
if err != nil {
|
||||
service.errorHandler(err, "001-F002", w)
|
||||
return
|
||||
}
|
||||
authResponse.UserID = userID
|
||||
authResponse.OK = true
|
||||
|
||||
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)
|
||||
w.Write(authResp)
|
||||
} // }}}
|
||||
|
Loading…
Reference in New Issue
Block a user