2022-12-31 15:34:38 -08:00
|
|
|
package auth
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
|
|
|
"time"
|
|
|
|
|
2023-06-03 07:18:47 -07:00
|
|
|
"codeberg.org/pronounscc/pronouns.cc/backend/db"
|
|
|
|
"codeberg.org/pronounscc/pronouns.cc/backend/server"
|
2022-12-31 15:34:38 -08:00
|
|
|
"emperror.dev/errors"
|
|
|
|
"github.com/go-chi/render"
|
|
|
|
"github.com/rs/xid"
|
|
|
|
)
|
|
|
|
|
|
|
|
type getTokenResponse struct {
|
2023-03-30 07:50:30 -07:00
|
|
|
TokenID xid.ID `json:"id"`
|
|
|
|
APIOnly bool `json:"api_only"`
|
|
|
|
ReadOnly bool `json:"read_only"`
|
|
|
|
Created time.Time `json:"created"`
|
|
|
|
Expires time.Time `json:"expires"`
|
2022-12-31 15:34:38 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
func dbTokenToGetResponse(t db.Token) getTokenResponse {
|
|
|
|
return getTokenResponse{
|
2023-03-30 07:50:30 -07:00
|
|
|
TokenID: t.TokenID,
|
|
|
|
APIOnly: t.APIOnly,
|
|
|
|
ReadOnly: t.ReadOnly,
|
|
|
|
Created: t.Created,
|
|
|
|
Expires: t.Expires,
|
2022-12-31 15:34:38 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Server) getTokens(w http.ResponseWriter, r *http.Request) error {
|
|
|
|
ctx := r.Context()
|
|
|
|
claims, _ := server.ClaimsFromContext(ctx)
|
|
|
|
|
2023-03-30 07:58:35 -07:00
|
|
|
if claims.APIToken {
|
|
|
|
return server.APIError{Code: server.ErrMissingPermissions, Details: "This endpoint cannot be used by API tokens"}
|
|
|
|
}
|
|
|
|
|
2022-12-31 15:34:38 -08:00
|
|
|
tokens, err := s.DB.Tokens(ctx, claims.UserID)
|
|
|
|
if err != nil {
|
|
|
|
return errors.Wrap(err, "getting tokens")
|
|
|
|
}
|
|
|
|
|
|
|
|
resps := make([]getTokenResponse, len(tokens))
|
|
|
|
for i := range tokens {
|
|
|
|
resps[i] = dbTokenToGetResponse(tokens[i])
|
|
|
|
}
|
|
|
|
|
|
|
|
render.JSON(w, r, resps)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Server) deleteToken(w http.ResponseWriter, r *http.Request) error {
|
|
|
|
ctx := r.Context()
|
|
|
|
claims, _ := server.ClaimsFromContext(ctx)
|
|
|
|
|
2023-03-30 07:50:30 -07:00
|
|
|
if claims.APIToken {
|
2023-03-30 07:58:35 -07:00
|
|
|
return server.APIError{Code: server.ErrMissingPermissions, Details: "This endpoint cannot be used by API tokens"}
|
2023-03-30 07:05:10 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
tx, err := s.DB.Begin(ctx)
|
2022-12-31 15:34:38 -08:00
|
|
|
if err != nil {
|
2023-03-30 07:05:10 -07:00
|
|
|
return errors.Wrap(err, "beginning transaction")
|
2023-03-13 09:01:36 -07:00
|
|
|
}
|
2023-03-30 07:05:10 -07:00
|
|
|
defer tx.Rollback(ctx)
|
2023-03-13 09:01:36 -07:00
|
|
|
|
2023-03-30 07:05:10 -07:00
|
|
|
err = s.DB.InvalidateAllTokens(ctx, tx, claims.UserID)
|
2023-03-13 09:01:36 -07:00
|
|
|
if err != nil {
|
2023-03-30 07:05:10 -07:00
|
|
|
return errors.Wrap(err, "invalidating tokens")
|
|
|
|
}
|
2023-03-13 09:01:36 -07:00
|
|
|
|
2023-03-30 07:05:10 -07:00
|
|
|
err = tx.Commit(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return errors.Wrap(err, "committing transaction")
|
2022-12-31 15:34:38 -08:00
|
|
|
}
|
|
|
|
|
2023-03-30 07:05:10 -07:00
|
|
|
render.NoContent(w, r)
|
2022-12-31 15:34:38 -08:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-03-30 07:50:30 -07:00
|
|
|
type createTokenResponse struct {
|
|
|
|
Token string `json:"token"`
|
|
|
|
TokenID xid.ID `json:"id"`
|
|
|
|
APIOnly bool `json:"api_only"`
|
|
|
|
ReadOnly bool `json:"read_only"`
|
|
|
|
Created time.Time `json:"created"`
|
|
|
|
Expires time.Time `json:"expires"`
|
|
|
|
}
|
|
|
|
|
2022-12-31 15:34:38 -08:00
|
|
|
func (s *Server) createToken(w http.ResponseWriter, r *http.Request) error {
|
2023-03-30 07:50:30 -07:00
|
|
|
ctx := r.Context()
|
|
|
|
claims, _ := server.ClaimsFromContext(ctx)
|
|
|
|
|
|
|
|
if claims.APIToken {
|
2023-03-30 07:58:35 -07:00
|
|
|
return server.APIError{Code: server.ErrMissingPermissions, Details: "This endpoint cannot be used by API tokens"}
|
2023-03-30 07:50:30 -07:00
|
|
|
}
|
|
|
|
|
2023-04-17 14:43:04 -07:00
|
|
|
u, err := s.DB.User(ctx, claims.UserID)
|
|
|
|
if err != nil {
|
|
|
|
return errors.Wrap(err, "getting me user")
|
|
|
|
}
|
|
|
|
|
2023-03-30 07:50:30 -07:00
|
|
|
readOnly := r.FormValue("read_only") == "true"
|
|
|
|
tokenID := xid.New()
|
2023-04-17 14:43:04 -07:00
|
|
|
tokenStr, err := s.Auth.CreateToken(claims.UserID, tokenID, u.IsAdmin, true, !readOnly)
|
2023-03-30 07:50:30 -07:00
|
|
|
if err != nil {
|
|
|
|
return errors.Wrap(err, "creating token")
|
|
|
|
}
|
|
|
|
|
|
|
|
t, err := s.DB.SaveToken(ctx, claims.UserID, tokenID, true, readOnly)
|
|
|
|
if err != nil {
|
|
|
|
return errors.Wrap(err, "saving token")
|
|
|
|
}
|
|
|
|
|
|
|
|
render.JSON(w, r, createTokenResponse{
|
|
|
|
Token: tokenStr,
|
|
|
|
TokenID: t.TokenID,
|
|
|
|
APIOnly: t.APIOnly,
|
|
|
|
ReadOnly: t.ReadOnly,
|
|
|
|
Created: t.Created,
|
|
|
|
Expires: t.Expires,
|
|
|
|
})
|
|
|
|
return nil
|
2022-12-31 15:34:38 -08:00
|
|
|
}
|