Compare commits

...

2 Commits

Author SHA1 Message Date
024948b8ff implemented extended gql errors 2021-09-18 23:50:00 +02:00
20f95b8ea4 added logout route 2021-09-18 19:33:45 +02:00
3 changed files with 45 additions and 2 deletions

25
internal/errors.go Normal file
View 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,
}
}

View File

@@ -114,12 +114,12 @@ func isAuth(ctx context.Context) (bool, error) {
token, ok := ctx.Value("jwt").(*jwt.Token)
if !ok {
return false, fmt.Errorf("Unauthorized")
return false, extendError("UNAUTHORIZED", "Unauthorized")
}
if token.Valid {
return true, nil
} else {
return false, fmt.Errorf("Unauthorized")
return false, extendError("UNAUTHORIZED", "Unauthorized")
}
}

View File

@@ -78,6 +78,8 @@ func initHttp(resolveContext context.Context, schema graphql.Schema, address str
r.HandleFunc("/api/cookie", setLoginCookie).Methods("POST")
r.HandleFunc("/api/logout", logout).Methods("POST")
// Init the embedded static files
initStatic(r)
@@ -242,3 +244,19 @@ func setLoginCookie(rw http.ResponseWriter, r *http.Request) {
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)
}