photoprism/internal/form/client_token.go
Michael Mayer 7a05c5553b OAuth2: Add "POST /api/v1/oauth/revoke" API endpoint #782 #808 #3943
Signed-off-by: Michael Mayer <michael@photoprism.app>
2024-01-10 12:21:43 +01:00

46 lines
946 B
Go

package form
import (
"fmt"
"github.com/photoprism/photoprism/pkg/rnd"
)
const (
ClientAccessToken = "access_token"
)
// ClientToken represents a client authentication token.
type ClientToken struct {
AuthToken string `form:"token" binding:"required" json:"token,omitempty"`
TypeHint string `form:"token_type_hint" json:" token_type_hint,omitempty"`
}
// Empty checks if all form values are unset.
func (f ClientToken) Empty() bool {
switch {
case f.AuthToken != "":
return false
case f.TypeHint != "":
return false
}
return true
}
// Validate checks the token and token type.
func (f ClientToken) Validate() error {
// Check auth token.
if f.AuthToken == "" {
return fmt.Errorf("missing token")
} else if !rnd.IsAlnum(f.AuthToken) {
return fmt.Errorf("invalid token")
}
// Check token type.
if f.TypeHint != "" && f.TypeHint != ClientAccessToken {
return fmt.Errorf("unsupported token type")
}
return nil
}