Added error code

This commit is contained in:
Magnus Åhall 2024-01-06 10:15:01 +01:00
parent 865aab95a9
commit c3571a7fdc
2 changed files with 31 additions and 29 deletions

26
pkg.go
View File

@ -54,9 +54,10 @@ const VERSION = "v0.1.0"
type HttpHandler func(http.ResponseWriter, *http.Request)
type ErrorHandler func(err error, w http.ResponseWriter)
type ErrorHandler func(err error, code string, w http.ResponseWriter)
type ServiceError struct {
OK bool
Code string
Error string
}
@ -115,10 +116,11 @@ func (service *Service) defaultAuthorizationHandler(sess *session.T, r *http.Req
service.logger.Debug("webservice", "op", "authorization", "session", sess.UUID, "request", r.URL.String(), "authorized", resp)
return
} // }}}
func (service *Service) defaultErrorHandler(err error, w http.ResponseWriter) { // {{{
func (service *Service) defaultErrorHandler(err error, code string, w http.ResponseWriter) { // {{{
service.logger.Error("webservice", "error", err)
errMsg := ServiceError{}
errMsg.OK = false
errMsg.Code = code
errMsg.Error = err.Error()
errJSON, _ := json.Marshal(errMsg)
w.Write(errJSON)
@ -158,7 +160,7 @@ func (service *Service) SetDatabase(sqlProv database.SqlProvider) { // {{{
} // }}}
func (service *Service) Register(path string, requireSession, requireAuthentication bool, handler ServiceHandler) { // {{{
http.HandleFunc(path, func(w http.ResponseWriter, r *http.Request) {
var session *session.T
var sess *session.T
var found bool
var authorized bool
var err error
@ -170,35 +172,35 @@ func (service *Service) Register(path string, requireSession, requireAuthenticat
if requireSession {
headerSessionUUID, err := sessionUUID(r)
if err != nil {
service.errorHandler(fmt.Errorf("Header X-Session-ID missing"), w)
service.errorHandler(fmt.Errorf("Header X-Session-ID missing"), "001-0000", w)
return
}
session, found = service.retrieveSession(headerSessionUUID)
sess, found = service.retrieveSession(headerSessionUUID)
if !found {
service.errorHandler(fmt.Errorf("Session '%s' not found", headerSessionUUID), w)
service.errorHandler(fmt.Errorf("Session '%s' not found", headerSessionUUID), "001-0001", w)
return
}
}
if requireAuthentication {
if !session.Authenticated {
service.errorHandler(fmt.Errorf("Session '%s' not authenticated", session.UUID), w)
if !sess.Authenticated {
service.errorHandler(fmt.Errorf("Session '%s' not authenticated", sess.UUID), "001-0002", w)
return
}
authorized, err = service.authorizationHandler(session, r)
authorized, err = service.authorizationHandler(sess, r)
if err != nil {
service.errorHandler(err, w)
service.errorHandler(err, "001-F001", w)
return
}
if !authorized {
service.errorHandler(fmt.Errorf("Session '%s' not authorized for %s", session.UUID, r.URL.String()), w)
service.errorHandler(fmt.Errorf("Session '%s' not authorized for %s", sess.UUID, r.URL.String()), "001-0003", w)
return
}
}
handler(w, r, session)
handler(w, r, sess)
})
} // }}}
func (service *Service) InitDatabaseConnection() (err error) { // {{{

View File

@ -28,31 +28,31 @@ type AuthenticationResponse struct {
type AuthenticationHandler func(AuthenticationRequest, bool) (AuthenticationResponse, error)
type AuthorizationHandler func(*session.T, *http.Request) (bool, error)
func (service *Service) sessionNew(w http.ResponseWriter, r *http.Request, sess *session.T) { // {{{
var session session.T
func (service *Service) sessionNew(w http.ResponseWriter, r *http.Request, foo *session.T) { // {{{
var sess session.T
var found bool
var err error
for {
session.UUID = uuid.NewString()
sess.UUID = uuid.NewString()
if service.Db == nil {
if _, found = service.sessions[session.UUID]; found {
if _, found = service.sessions[sess.UUID]; found {
continue
}
session.Authenticated = false
session.Created = time.Now()
service.sessions[session.UUID] = &session
sess.Authenticated = false
sess.Created = time.Now()
service.sessions[sess.UUID] = &sess
break
} else {
if _, found = service.retrieveSession(session.UUID); found {
if _, found = service.retrieveSession(sess.UUID); found {
continue
}
err = service.Db.NewSession(session.UUID)
err = service.Db.NewSession(sess.UUID)
if err != nil {
service.errorHandler(err, w)
service.errorHandler(err, "001-A001", w)
return
}
break
@ -60,15 +60,15 @@ func (service *Service) sessionNew(w http.ResponseWriter, r *http.Request, sess
}
service.logger.Info("session", "op", "new", "uuid", session.UUID)
service.logger.Info("session", "op", "new", "uuid", sess.UUID)
respJSON, _ := json.Marshal(
struct {
OK bool
UUID string
Session session.T
}{
true,
session.UUID,
sess,
},
)
@ -84,7 +84,7 @@ func (service *Service) sessionAuthenticate(w http.ResponseWriter, r *http.Reque
var authRequest AuthenticationRequest
err = json.Unmarshal(reqBody, &authRequest)
if err != nil {
service.errorHandler(err, w)
service.errorHandler(err, "001-0004", w)
return
}
@ -93,14 +93,14 @@ func (service *Service) sessionAuthenticate(w http.ResponseWriter, r *http.Reque
if service.Db != nil {
authenticated, userID, err = service.Db.Authenticate(authRequest.Username, authRequest.Password)
if err != nil {
service.errorHandler(err, w)
service.errorHandler(err, "001-A002", w)
return
}
if authenticated && userID > 0 {
err = service.Db.SetSessionUser(sess.UUID, userID)
if err != nil {
service.errorHandler(err, w)
service.errorHandler(err, "001-A003", w)
return
}
}
@ -111,7 +111,7 @@ func (service *Service) sessionAuthenticate(w http.ResponseWriter, r *http.Reque
// and given a chance to override it.
authResponse, err = service.authenticationHandler(authRequest, authenticated)
if err != nil {
service.errorHandler(err, w)
service.errorHandler(err, "001-F002", w)
return
}
authResponse.UserID = userID