feat(server): add /api/v1/session endpoint
All checks were successful
arcad/emissary/pipeline/head This commit looks good
All checks were successful
arcad/emissary/pipeline/head This commit looks good
This commit is contained in:
@ -1,6 +1,7 @@
|
||||
package agent
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
|
||||
"forge.cadoles.com/Cadoles/emissary/internal/auth"
|
||||
@ -29,4 +30,18 @@ func (u *User) Agent() *datastore.Agent {
|
||||
return u.agent
|
||||
}
|
||||
|
||||
func (u *User) MarshalJSON() ([]byte, error) {
|
||||
type user struct {
|
||||
Subject string `json:"subject"`
|
||||
Tenant string `json:"tenant"`
|
||||
}
|
||||
|
||||
jsonUser := user{
|
||||
Subject: u.Subject(),
|
||||
Tenant: string(u.Tenant()),
|
||||
}
|
||||
|
||||
return json.Marshal(jsonUser)
|
||||
}
|
||||
|
||||
var _ auth.User = &User{}
|
||||
|
@ -64,16 +64,16 @@ func Middleware(authenticators ...Authenticator) func(http.Handler) http.Handler
|
||||
}
|
||||
|
||||
if user == nil {
|
||||
isUnauthorized, isUnauthenticated, isUnknown := checkErrors(errs)
|
||||
hasUnauthorized, hasUnauthenticated, hasUnknown := checkErrors(errs)
|
||||
|
||||
switch {
|
||||
case isUnauthorized && !isUnknown:
|
||||
case hasUnauthorized && !hasUnknown:
|
||||
api.ErrorResponse(w, http.StatusForbidden, api.ErrCodeForbidden, nil)
|
||||
return
|
||||
case isUnauthenticated && !isUnknown:
|
||||
api.ErrorResponse(w, http.StatusForbidden, api.ErrCodeForbidden, nil)
|
||||
case hasUnauthenticated && !hasUnknown:
|
||||
api.ErrorResponse(w, http.StatusUnauthorized, api.ErrCodeUnauthorized, nil)
|
||||
return
|
||||
case isUnknown:
|
||||
case hasUnknown:
|
||||
api.ErrorResponse(w, http.StatusInternalServerError, api.ErrCodeUnknownError, nil)
|
||||
return
|
||||
default:
|
||||
@ -101,10 +101,8 @@ func checkErrors(errs []error) (isUnauthorized bool, isUnauthenticated bool, isU
|
||||
switch {
|
||||
case errors.Is(e, ErrUnauthorized):
|
||||
isUnauthorized = true
|
||||
continue
|
||||
case errors.Is(e, ErrUnauthenticated):
|
||||
isUnauthenticated = true
|
||||
continue
|
||||
default:
|
||||
isUnknown = true
|
||||
}
|
||||
|
@ -1,6 +1,8 @@
|
||||
package user
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
|
||||
"forge.cadoles.com/Cadoles/emissary/internal/auth"
|
||||
"forge.cadoles.com/Cadoles/emissary/internal/datastore"
|
||||
)
|
||||
@ -39,4 +41,20 @@ func (u *User) Role() Role {
|
||||
return u.role
|
||||
}
|
||||
|
||||
func (u *User) MarshalJSON() ([]byte, error) {
|
||||
type user struct {
|
||||
Subject string `json:"subject"`
|
||||
Tenant string `json:"tenant"`
|
||||
Role string `json:"role"`
|
||||
}
|
||||
|
||||
jsonUser := user{
|
||||
Subject: u.Subject(),
|
||||
Tenant: string(u.Tenant()),
|
||||
Role: string(u.Role()),
|
||||
}
|
||||
|
||||
return json.Marshal(jsonUser)
|
||||
}
|
||||
|
||||
var _ auth.User = &User{}
|
||||
|
Reference in New Issue
Block a user