Compare commits
2 Commits
9f280caafc
...
024948b8ff
| Author | SHA1 | Date | |
|---|---|---|---|
| 024948b8ff | |||
| 20f95b8ea4 |
25
internal/errors.go
Normal file
25
internal/errors.go
Normal file
@@ -0,0 +1,25 @@
|
|||||||
|
package s3browser
|
||||||
|
|
||||||
|
import "fmt"
|
||||||
|
|
||||||
|
type extendedError struct {
|
||||||
|
Message string
|
||||||
|
Code string
|
||||||
|
}
|
||||||
|
|
||||||
|
func (err *extendedError) Error() string {
|
||||||
|
return err.Message
|
||||||
|
}
|
||||||
|
|
||||||
|
func (err *extendedError) Extensions() map[string]interface{} {
|
||||||
|
return map[string]interface{}{
|
||||||
|
"code": err.Code,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func extendError(code, format string, a ...interface{}) *extendedError {
|
||||||
|
return &extendedError{
|
||||||
|
Message: fmt.Sprintf(format, a...),
|
||||||
|
Code: code,
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -114,12 +114,12 @@ func isAuth(ctx context.Context) (bool, error) {
|
|||||||
token, ok := ctx.Value("jwt").(*jwt.Token)
|
token, ok := ctx.Value("jwt").(*jwt.Token)
|
||||||
|
|
||||||
if !ok {
|
if !ok {
|
||||||
return false, fmt.Errorf("Unauthorized")
|
return false, extendError("UNAUTHORIZED", "Unauthorized")
|
||||||
}
|
}
|
||||||
|
|
||||||
if token.Valid {
|
if token.Valid {
|
||||||
return true, nil
|
return true, nil
|
||||||
} else {
|
} else {
|
||||||
return false, fmt.Errorf("Unauthorized")
|
return false, extendError("UNAUTHORIZED", "Unauthorized")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -78,6 +78,8 @@ func initHttp(resolveContext context.Context, schema graphql.Schema, address str
|
|||||||
|
|
||||||
r.HandleFunc("/api/cookie", setLoginCookie).Methods("POST")
|
r.HandleFunc("/api/cookie", setLoginCookie).Methods("POST")
|
||||||
|
|
||||||
|
r.HandleFunc("/api/logout", logout).Methods("POST")
|
||||||
|
|
||||||
// Init the embedded static files
|
// Init the embedded static files
|
||||||
initStatic(r)
|
initStatic(r)
|
||||||
|
|
||||||
@@ -242,3 +244,19 @@ func setLoginCookie(rw http.ResponseWriter, r *http.Request) {
|
|||||||
|
|
||||||
rw.WriteHeader(http.StatusNoContent)
|
rw.WriteHeader(http.StatusNoContent)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//logout removes the jwt cookie
|
||||||
|
func logout(rw http.ResponseWriter, r *http.Request) {
|
||||||
|
cookie := &http.Cookie{
|
||||||
|
Name: "jwt",
|
||||||
|
Value: "",
|
||||||
|
Path: "/api",
|
||||||
|
Expires: time.Unix(0, 0),
|
||||||
|
HttpOnly: true,
|
||||||
|
SameSite: http.SameSiteStrictMode,
|
||||||
|
}
|
||||||
|
|
||||||
|
http.SetCookie(rw, cookie)
|
||||||
|
|
||||||
|
rw.WriteHeader(http.StatusNoContent)
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user