auth/internal/domain/scope.go

201 lines
4.7 KiB
Go
Raw Normal View History

2021-12-25 18:55:59 +00:00
package domain
import (
"fmt"
2021-12-29 20:08:30 +00:00
"strconv"
2021-12-25 18:55:59 +00:00
"strings"
"source.toby3d.me/toby3d/auth/internal/common"
2021-12-25 18:55:59 +00:00
)
type (
2022-01-29 17:50:45 +00:00
// Scope represent single token scope supported by IndieAuth.
//
// NOTE(toby3d): Encapsulate enums in structs for extra compile-time safety:
// https://threedots.tech/post/safer-enums-in-go/#struct-based-enums
2021-12-25 18:55:59 +00:00
Scope struct {
2022-01-29 17:50:45 +00:00
uid string
2021-12-25 18:55:59 +00:00
}
2021-12-29 20:08:30 +00:00
// Scopes represent set of Scope domains.
2021-12-25 18:55:59 +00:00
Scopes []Scope
)
var ErrScopeUnknown error = NewError(ErrorCodeInvalidRequest, "unknown scope", "https://indieweb.org/scope")
2021-12-25 18:55:59 +00:00
2022-12-26 14:09:34 +00:00
//nolint:gochecknoglobals // structs cannot be constants
2021-12-25 18:55:59 +00:00
var (
ScopeUnd = Scope{uid: ""} // "und"
2021-12-25 18:55:59 +00:00
// https://indieweb.org/scope#Micropub_Scopes
ScopeCreate = Scope{uid: "create"} // "create"
ScopeDelete = Scope{uid: "delete"} // "delete"
ScopeDraft = Scope{uid: "draft"} // "draft"
ScopeMedia = Scope{uid: "media"} // "media"
ScopeUndelete = Scope{uid: "undelete"} // "undelete"
ScopeUpdate = Scope{uid: "update"} // "update"
2021-12-25 18:55:59 +00:00
// https://indieweb.org/scope#Microsub_Scopes
ScopeBlock = Scope{uid: "block"} // "block"
ScopeChannels = Scope{uid: "channels"} // "channels"
ScopeFollow = Scope{uid: "follow"} // "follow"
ScopeMute = Scope{uid: "mute"} // "mute"
ScopeRead = Scope{uid: "read"} // "read"
2021-12-25 18:55:59 +00:00
// This scope requests access to the user's default profile information
2022-02-02 17:52:52 +00:00
// which include the following properties: name, photo, url.
2021-12-25 18:55:59 +00:00
//
// NOTE(toby3d): https://indieauth.net/source/#profile-information
ScopeProfile = Scope{uid: "profile"} // "profile"
2021-12-25 18:55:59 +00:00
// This scope requests access to the user's email address in the
// following property: email.
//
// Note that because the profile scope is required when requesting
// profile information, the email scope cannot be requested on its own
// and must be requested along with the profile scope if desired.
//
// NOTE(toby3d): https://indieauth.net/source/#profile-information
ScopeEmail = Scope{uid: "email"} // "email"
2021-12-25 18:55:59 +00:00
)
2022-12-26 14:09:34 +00:00
//nolint:gochecknoglobals // maps cannot be constants
2022-01-29 17:50:45 +00:00
var uidsScopes = map[string]Scope{
ScopeBlock.uid: ScopeBlock,
ScopeChannels.uid: ScopeChannels,
ScopeCreate.uid: ScopeCreate,
ScopeDelete.uid: ScopeDelete,
ScopeDraft.uid: ScopeDraft,
ScopeEmail.uid: ScopeEmail,
ScopeFollow.uid: ScopeFollow,
ScopeMedia.uid: ScopeMedia,
ScopeMute.uid: ScopeMute,
ScopeProfile.uid: ScopeProfile,
ScopeRead.uid: ScopeRead,
2022-03-07 18:30:16 +00:00
ScopeUndelete.uid: ScopeUndelete,
2022-01-29 17:50:45 +00:00
ScopeUpdate.uid: ScopeUpdate,
2021-12-25 18:55:59 +00:00
}
// ParseScope parses scope slug into Scope domain.
2022-01-29 17:50:45 +00:00
func ParseScope(uid string) (Scope, error) {
if scope, ok := uidsScopes[strings.ToLower(uid)]; ok {
2021-12-25 18:55:59 +00:00
return scope, nil
}
return ScopeUnd, fmt.Errorf("%w: %s", ErrScopeUnknown, uid)
2021-12-25 18:55:59 +00:00
}
func (s Scope) MarshalJSON() ([]byte, error) {
return []byte(strconv.Quote(s.uid)), nil
}
// String returns string representation of scope.
func (s Scope) String() string {
if s.uid != "" {
return s.uid
}
return common.Und
}
func (s Scope) GoString() string {
return "domain.Scope(" + s.String() + ")"
}
2022-01-29 17:50:45 +00:00
// UnmarshalForm implements custom unmarshler for form values.
2022-01-12 18:04:40 +00:00
func (s *Scopes) UnmarshalForm(v []byte) error {
scopes := make(Scopes, 0)
2021-12-25 18:55:59 +00:00
2022-01-12 18:04:40 +00:00
for _, rawScope := range strings.Fields(string(v)) {
scope, err := ParseScope(rawScope)
if err != nil {
return fmt.Errorf("Scopes: UnmarshalForm: %w", err)
2022-01-29 17:50:45 +00:00
}
if scopes.Has(scope) {
continue
2022-01-12 18:04:40 +00:00
}
2021-12-29 20:08:30 +00:00
2022-01-29 17:50:45 +00:00
scopes = append(scopes, scope)
2021-12-29 20:08:30 +00:00
}
2022-01-12 18:04:40 +00:00
*s = scopes
2021-12-29 20:08:30 +00:00
return nil
}
2022-01-29 17:50:45 +00:00
// UnmarshalJSON implements custom unmarshler for JSON.
2021-12-29 20:08:30 +00:00
func (s *Scopes) UnmarshalJSON(v []byte) error {
src, err := strconv.Unquote(string(v))
if err != nil {
return fmt.Errorf("Scopes: UnmarshalJSON: %w", err)
2021-12-29 20:08:30 +00:00
}
2022-01-29 17:50:45 +00:00
result := make(Scopes, 0)
2021-12-29 20:08:30 +00:00
for _, rawScope := range strings.Fields(src) {
scope, err := ParseScope(rawScope)
2021-12-29 20:08:30 +00:00
if err != nil {
return fmt.Errorf("Scopes: UnmarshalJSON: %w", err)
2022-01-29 17:50:45 +00:00
}
if result.Has(scope) {
2022-01-29 17:50:45 +00:00
continue
2021-12-29 20:08:30 +00:00
}
result = append(result, scope)
2021-12-29 20:08:30 +00:00
}
*s = result
return nil
}
2022-01-29 17:50:45 +00:00
// UnmarshalJSON implements custom marshler for JSON.
func (s Scopes) MarshalJSON() ([]byte, error) {
scopes := make([]string, len(s))
for i := range s {
scopes[i] = s[i].String()
}
return []byte(strconv.Quote(strings.Join(scopes, " "))), nil
}
2022-01-29 17:50:45 +00:00
// String returns string representation of scopes.
2021-12-29 20:08:30 +00:00
func (s Scopes) String() string {
scopes := make([]string, len(s))
for i := range s {
scopes[i] = s[i].String()
}
return strings.Join(scopes, " ")
}
2022-01-29 17:50:45 +00:00
// IsEmpty returns true if the set does not contain valid scope.
func (s Scopes) IsEmpty() bool {
for i := range s {
if s[i] == ScopeUnd {
2022-01-29 17:50:45 +00:00
continue
}
return false
}
return true
}
// Has check what input scope contains in current scopes collection.
func (s Scopes) Has(scope Scope) bool {
for i := range s {
if s[i] != scope {
continue
}
return true
}
return false
}