Access tokens, server, moving things

- Added placeholder funcs for access tokens
- Moved an error definition and added another constant
- Changed (passkey) auth fail to return a json error for now
- TODO: Change that into checking for a provided access token before
  failing
This commit is contained in:
Melody Becker 2024-10-15 20:41:23 +02:00
parent 4f761c20c0
commit 391d8b1b48
5 changed files with 56 additions and 6 deletions

View file

@ -13,4 +13,5 @@ const (
HttpErrIdPlaceholder = iota HttpErrIdPlaceholder = iota
HttpErrIdMissingContextValue HttpErrIdMissingContextValue
HttpErrIdDbFailure HttpErrIdDbFailure
HttpErrIdNotAuthenticated
) )

View file

@ -4,10 +4,10 @@ import (
"fmt" "fmt"
"io/fs" "io/fs"
"net/http" "net/http"
"net/url"
"github.com/mstarongithub/passkey" "github.com/mstarongithub/passkey"
"github.com/rs/zerolog/log" "github.com/rs/zerolog/log"
"gitlab.com/mstarongitlab/goutils/other"
"gitlab.com/mstarongitlab/linstrom/storage" "gitlab.com/mstarongitlab/linstrom/storage"
) )
@ -38,7 +38,14 @@ func buildRootHandler(pkey *passkey.Passkey, reactiveFS, staticFS fs.FS) http.Ha
pkey.Auth( pkey.Auth(
ContextKeyPasskeyUsername, ContextKeyPasskeyUsername,
nil, nil,
passkey.RedirectUnauthorized(url.URL{Path: "/"}), func(w http.ResponseWriter, r *http.Request) {
other.HttpErr(
w,
HttpErrIdNotAuthenticated,
"Not authenticated",
http.StatusUnauthorized,
)
},
)(ChainMiddlewares(setupTestEndpoints(), passkeyIdToAccountIdTransformerMiddleware)), )(ChainMiddlewares(setupTestEndpoints(), passkeyIdToAccountIdTransformerMiddleware)),
) )

44
storage/accessTokens.go Normal file
View file

@ -0,0 +1,44 @@
package storage
import (
"time"
"gorm.io/gorm"
)
type AccessToken struct {
gorm.Model
BelongsToUserId string
Name string
Token string
ExpiresAt time.Time
}
func (s *Storage) GetTokensForAccId(accId uint) ([]AccessToken, error) {
// TODO: Implement me
panic("Not implemented")
}
func (s *Storage) NewAccessToken(
forAccId uint,
name string,
expiresAt time.Time,
) (*AccessToken, error) {
// TODO: Implement me
panic("Not implemented")
}
func (s *Storage) ExtendToken(accId uint, newExpiry time.Time) error {
// TODO: Implement me
panic("Not implemented")
}
func (s *Storage) RenameToken(accId, oldName string, newName string) error {
// TODO: Implement me
panic("Not implemented")
}
func (s *Storage) DiscardToken(accId uint, name string) error {
// TODO: Implement me
panic("Not implemented")
}

View file

@ -11,3 +11,4 @@ func (n ErrNotImplemented) Error() string {
var ErrEntryNotFound = errors.New("entry not found") var ErrEntryNotFound = errors.New("entry not found")
var ErrEntryAlreadyExists = errors.New("entry already exists") var ErrEntryAlreadyExists = errors.New("entry already exists")
var ErrNothingToChange = errors.New("nothing to change") var ErrNothingToChange = errors.New("nothing to change")
var ErrInvalidData = errors.New("invalid data")

View file

@ -6,8 +6,6 @@
package storage package storage
import ( import (
"errors"
"github.com/rs/zerolog/log" "github.com/rs/zerolog/log"
"gitlab.com/mstarongitlab/linstrom/storage/cache" "gitlab.com/mstarongitlab/linstrom/storage/cache"
"gorm.io/driver/postgres" "gorm.io/driver/postgres"
@ -21,8 +19,6 @@ type Storage struct {
cache *cache.Cache cache *cache.Cache
} }
var ErrInvalidData = errors.New("invalid data")
func NewStorage(dbUrl string, cache *cache.Cache) (*Storage, error) { func NewStorage(dbUrl string, cache *cache.Cache) (*Storage, error) {
db, err := gorm.Open(postgres.Open(dbUrl), &gorm.Config{ db, err := gorm.Open(postgres.Open(dbUrl), &gorm.Config{
Logger: newGormLogger(log.Logger), Logger: newGormLogger(log.Logger),
@ -39,6 +35,7 @@ func NewStorage(dbUrl string, cache *cache.Cache) (*Storage, error) {
PasskeySession{}, PasskeySession{},
InboundJob{}, InboundJob{},
OutboundJob{}, OutboundJob{},
AccessToken{},
) )
if err != nil { if err != nil {
return nil, err return nil, err