feat(identity): name, lifetime and last-used on personal API tokens
This commit is contained in:
23 files changed
+449
-99
No files matched your search
@@ -1,12 +1,18 @@
|
||||
package webui
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
"strings"
|
||||
"time"
|
||||
"unicode/utf8"
|
||||
|
||||
"github.com/samcharles93/archie-core/internal/domain/access"
|
||||
"github.com/samcharles93/archie-core/internal/domain/identity"
|
||||
)
|
||||
|
||||
const maxTokenNameLength = 40
|
||||
|
||||
// Personal API tokens belong to the signed-in person; the State Store acts
|
||||
// on the principal the request carries, so these handlers name no one.
|
||||
|
||||
@@ -35,12 +41,35 @@ func (s *Server) handlePersonalTokenCreate(w http.ResponseWriter, r *http.Reques
|
||||
if !s.personalTokensReady(w, r) {
|
||||
return
|
||||
}
|
||||
var body struct {
|
||||
Name string `json:"name"`
|
||||
Days int `json:"days"`
|
||||
}
|
||||
if err := json.NewDecoder(http.MaxBytesReader(w, r.Body, 4<<10)).Decode(&body); err != nil {
|
||||
http.Error(w, "invalid request", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
name := strings.TrimSpace(body.Name)
|
||||
if name == "" || utf8.RuneCountInString(name) > maxTokenNameLength {
|
||||
http.Error(w, "Name a token in up to 40 characters.", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
var expiresAt *time.Time
|
||||
switch body.Days {
|
||||
case 0:
|
||||
case 7, 30, 90:
|
||||
t := time.Now().UTC().AddDate(0, 0, body.Days)
|
||||
expiresAt = &t
|
||||
default:
|
||||
http.Error(w, "Lifetime is 7, 30 or 90 days, or none.", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
raw, subject, err := identity.NewPersonalToken()
|
||||
if err != nil {
|
||||
http.Error(w, "cannot create token", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
if err := s.PersonalTokens.AddPersonalToken(r.Context(), subject); err != nil {
|
||||
if err := s.PersonalTokens.AddPersonalToken(r.Context(), subject, name, expiresAt); err != nil {
|
||||
writeIdentityError(w, err)
|
||||
return
|
||||
}
|
||||
|
||||
@@ -6,6 +6,7 @@ import (
|
||||
"net/http/httptest"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/samcharles93/archie-core/internal/domain/access"
|
||||
"github.com/samcharles93/archie-core/internal/domain/identity"
|
||||
@@ -65,7 +66,7 @@ type recordingTokens struct{ owner identity.IdentityID }
|
||||
|
||||
// AddPersonalToken mirrors the State Store's rule: only a signed-in person's
|
||||
// principal may own a personal token.
|
||||
func (t *recordingTokens) AddPersonalToken(ctx context.Context, _ identity.Subject) error {
|
||||
func (t *recordingTokens) AddPersonalToken(ctx context.Context, _ identity.Subject, _ string, _ *time.Time) error {
|
||||
p, ok := access.PrincipalFromContext(ctx)
|
||||
if !ok || p.IdentityID == identity.SystemID || p.Role("") != org.RoleOwner {
|
||||
return identity.ErrInvalid
|
||||
@@ -120,7 +121,7 @@ func TestSharedTokenOperatorPostsPersonalToken(t *testing.T) {
|
||||
}
|
||||
handler := s.requireToken(s.authorize(http.HandlerFunc(s.handlePersonalTokenCreate)))
|
||||
rec := httptest.NewRecorder()
|
||||
req := httptest.NewRequestWithContext(t.Context(), http.MethodPost, "/api/tokens", nil)
|
||||
req := httptest.NewRequestWithContext(t.Context(), http.MethodPost, "/api/tokens", strings.NewReader(`{"name":"ci","days":30}`))
|
||||
req.Header.Set("Authorization", "Bearer shared")
|
||||
handler.ServeHTTP(rec, req)
|
||||
if rec.Code != http.StatusCreated {
|
||||
|
||||
Reference in new issue
Block a user