Added error code
This commit is contained in:
parent
865aab95a9
commit
c3571a7fdc
26
pkg.go
26
pkg.go
@ -54,9 +54,10 @@ const VERSION = "v0.1.0"
|
|||||||
|
|
||||||
type HttpHandler func(http.ResponseWriter, *http.Request)
|
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 {
|
type ServiceError struct {
|
||||||
OK bool
|
OK bool
|
||||||
|
Code string
|
||||||
Error 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)
|
service.logger.Debug("webservice", "op", "authorization", "session", sess.UUID, "request", r.URL.String(), "authorized", resp)
|
||||||
return
|
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)
|
service.logger.Error("webservice", "error", err)
|
||||||
errMsg := ServiceError{}
|
errMsg := ServiceError{}
|
||||||
errMsg.OK = false
|
errMsg.OK = false
|
||||||
|
errMsg.Code = code
|
||||||
errMsg.Error = err.Error()
|
errMsg.Error = err.Error()
|
||||||
errJSON, _ := json.Marshal(errMsg)
|
errJSON, _ := json.Marshal(errMsg)
|
||||||
w.Write(errJSON)
|
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) { // {{{
|
func (service *Service) Register(path string, requireSession, requireAuthentication bool, handler ServiceHandler) { // {{{
|
||||||
http.HandleFunc(path, func(w http.ResponseWriter, r *http.Request) {
|
http.HandleFunc(path, func(w http.ResponseWriter, r *http.Request) {
|
||||||
var session *session.T
|
var sess *session.T
|
||||||
var found bool
|
var found bool
|
||||||
var authorized bool
|
var authorized bool
|
||||||
var err error
|
var err error
|
||||||
@ -170,35 +172,35 @@ func (service *Service) Register(path string, requireSession, requireAuthenticat
|
|||||||
if requireSession {
|
if requireSession {
|
||||||
headerSessionUUID, err := sessionUUID(r)
|
headerSessionUUID, err := sessionUUID(r)
|
||||||
if err != nil {
|
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
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
session, found = service.retrieveSession(headerSessionUUID)
|
sess, found = service.retrieveSession(headerSessionUUID)
|
||||||
if !found {
|
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
|
return
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if requireAuthentication {
|
if requireAuthentication {
|
||||||
if !session.Authenticated {
|
if !sess.Authenticated {
|
||||||
service.errorHandler(fmt.Errorf("Session '%s' not authenticated", session.UUID), w)
|
service.errorHandler(fmt.Errorf("Session '%s' not authenticated", sess.UUID), "001-0002", w)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
authorized, err = service.authorizationHandler(session, r)
|
authorized, err = service.authorizationHandler(sess, r)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
service.errorHandler(err, w)
|
service.errorHandler(err, "001-F001", w)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
if !authorized {
|
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
|
return
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
handler(w, r, session)
|
handler(w, r, sess)
|
||||||
})
|
})
|
||||||
} // }}}
|
} // }}}
|
||||||
func (service *Service) InitDatabaseConnection() (err error) { // {{{
|
func (service *Service) InitDatabaseConnection() (err error) { // {{{
|
||||||
|
34
session.go
34
session.go
@ -28,31 +28,31 @@ type AuthenticationResponse struct {
|
|||||||
type AuthenticationHandler func(AuthenticationRequest, bool) (AuthenticationResponse, error)
|
type AuthenticationHandler func(AuthenticationRequest, bool) (AuthenticationResponse, error)
|
||||||
type AuthorizationHandler func(*session.T, *http.Request) (bool, error)
|
type AuthorizationHandler func(*session.T, *http.Request) (bool, error)
|
||||||
|
|
||||||
func (service *Service) sessionNew(w http.ResponseWriter, r *http.Request, sess *session.T) { // {{{
|
func (service *Service) sessionNew(w http.ResponseWriter, r *http.Request, foo *session.T) { // {{{
|
||||||
var session session.T
|
var sess session.T
|
||||||
var found bool
|
var found bool
|
||||||
var err error
|
var err error
|
||||||
|
|
||||||
for {
|
for {
|
||||||
session.UUID = uuid.NewString()
|
sess.UUID = uuid.NewString()
|
||||||
if service.Db == nil {
|
if service.Db == nil {
|
||||||
if _, found = service.sessions[session.UUID]; found {
|
if _, found = service.sessions[sess.UUID]; found {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
session.Authenticated = false
|
sess.Authenticated = false
|
||||||
session.Created = time.Now()
|
sess.Created = time.Now()
|
||||||
service.sessions[session.UUID] = &session
|
service.sessions[sess.UUID] = &sess
|
||||||
break
|
break
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
if _, found = service.retrieveSession(session.UUID); found {
|
if _, found = service.retrieveSession(sess.UUID); found {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
err = service.Db.NewSession(session.UUID)
|
err = service.Db.NewSession(sess.UUID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
service.errorHandler(err, w)
|
service.errorHandler(err, "001-A001", w)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
break
|
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(
|
respJSON, _ := json.Marshal(
|
||||||
struct {
|
struct {
|
||||||
OK bool
|
OK bool
|
||||||
UUID string
|
Session session.T
|
||||||
}{
|
}{
|
||||||
true,
|
true,
|
||||||
session.UUID,
|
sess,
|
||||||
},
|
},
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -84,7 +84,7 @@ func (service *Service) sessionAuthenticate(w http.ResponseWriter, r *http.Reque
|
|||||||
var authRequest AuthenticationRequest
|
var authRequest AuthenticationRequest
|
||||||
err = json.Unmarshal(reqBody, &authRequest)
|
err = json.Unmarshal(reqBody, &authRequest)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
service.errorHandler(err, w)
|
service.errorHandler(err, "001-0004", w)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -93,14 +93,14 @@ func (service *Service) sessionAuthenticate(w http.ResponseWriter, r *http.Reque
|
|||||||
if service.Db != nil {
|
if service.Db != nil {
|
||||||
authenticated, userID, err = service.Db.Authenticate(authRequest.Username, authRequest.Password)
|
authenticated, userID, err = service.Db.Authenticate(authRequest.Username, authRequest.Password)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
service.errorHandler(err, w)
|
service.errorHandler(err, "001-A002", w)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
if authenticated && userID > 0 {
|
if authenticated && userID > 0 {
|
||||||
err = service.Db.SetSessionUser(sess.UUID, userID)
|
err = service.Db.SetSessionUser(sess.UUID, userID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
service.errorHandler(err, w)
|
service.errorHandler(err, "001-A003", w)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -111,7 +111,7 @@ func (service *Service) sessionAuthenticate(w http.ResponseWriter, r *http.Reque
|
|||||||
// 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, authenticated)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
service.errorHandler(err, w)
|
service.errorHandler(err, "001-F002", w)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
authResponse.UserID = userID
|
authResponse.UserID = userID
|
||||||
|
Loading…
Reference in New Issue
Block a user