drone/internal/api/handler/user/createPat.go
Johannes Batzill 8c2f900c80 Principals, ServiceAccounts, Tokens and auth.Sessions (#15)
This change introduces the concept of a principal (abstraction of call identity), and adds a new service account type principal. Also adds support for different tokens (session, PAT, SAT, OAuth2) and adds auth.Session which is being used to capture information about the caller and call method.
2022-09-25 23:44:51 -07:00

54 lines
1.6 KiB
Go

// Copyright 2021 Harness Inc. All rights reserved.
// Use of this source code is governed by the Polyform Free Trial License
// that can be found in the LICENSE.md file for this repository.
package user
import (
"encoding/json"
"net/http"
"time"
"github.com/harness/gitness/internal/api/render"
"github.com/harness/gitness/internal/api/request"
"github.com/harness/gitness/internal/store"
"github.com/harness/gitness/internal/token"
"github.com/harness/gitness/types"
"github.com/harness/gitness/types/enum"
"github.com/rs/zerolog/hlog"
)
type createPatRequest struct {
Name string `json:"name"`
LifeTime time.Duration `json:"lifetime"`
Grants enum.AccessGrant `json:"grants"`
}
// HandleCreatePAT returns an http.HandlerFunc that creates a new PAT and
// writes a json-encoded TokenResponse to the http.Response body.
func HandleCreatePAT(tokenStore store.TokenStore) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
log := hlog.FromRequest(r)
ctx := r.Context()
principal, _ := request.PrincipalFrom(ctx)
user, _ := request.UserFrom(ctx)
in := new(createPatRequest)
err := json.NewDecoder(r.Body).Decode(in)
if err != nil {
render.BadRequestf(w, "Invalid request body: %s.", err)
return
}
token, jwtToken, err := token.CreatePAT(ctx, tokenStore, principal, user, in.Name, in.LifeTime, in.Grants)
if err != nil {
log.Err(err).Msg("failed to create pat")
render.UserfiedErrorOrInternal(w, err)
return
}
render.JSON(w, http.StatusOK, &types.TokenResponse{Token: *token, AccessToken: jwtToken})
}
}