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"
|
|
|
|
)
|
|
|
|
|
|
|
|
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
|
|
|
|
)
|
|
|
|
|
2022-01-29 19:30:57 +00:00
|
|
|
var ErrScopeUnknown error = NewError(ErrorCodeInvalidRequest, "unknown scope", "https://indieweb.org/scope")
|
2021-12-25 18:55:59 +00:00
|
|
|
|
2022-02-01 17:27:48 +00:00
|
|
|
//nolint: gochecknoglobals // structs cannot be constants
|
2021-12-25 18:55:59 +00:00
|
|
|
var (
|
2022-01-29 17:50:45 +00:00
|
|
|
ScopeUndefined = Scope{uid: ""}
|
2021-12-25 18:55:59 +00:00
|
|
|
|
|
|
|
// https://indieweb.org/scope#Micropub_Scopes
|
2022-01-29 17:50:45 +00:00
|
|
|
ScopeCreate = Scope{uid: "create"}
|
|
|
|
ScopeDelete = Scope{uid: "delete"}
|
|
|
|
ScopeDraft = Scope{uid: "draft"}
|
|
|
|
ScopeMedia = Scope{uid: "media"}
|
|
|
|
ScopeUpdate = Scope{uid: "update"}
|
2021-12-25 18:55:59 +00:00
|
|
|
|
|
|
|
// https://indieweb.org/scope#Microsub_Scopes
|
2022-01-29 17:50:45 +00:00
|
|
|
ScopeBlock = Scope{uid: "block"}
|
|
|
|
ScopeChannels = Scope{uid: "channels"}
|
|
|
|
ScopeFollow = Scope{uid: "follow"}
|
|
|
|
ScopeMute = Scope{uid: "mute"}
|
|
|
|
ScopeRead = Scope{uid: "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
|
2022-01-29 17:50:45 +00:00
|
|
|
ScopeProfile = Scope{uid: "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
|
2022-01-29 17:50:45 +00:00
|
|
|
ScopeEmail = Scope{uid: "email"}
|
2021-12-25 18:55:59 +00:00
|
|
|
)
|
|
|
|
|
2022-02-01 17:27:48 +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,
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2022-01-29 17:50:45 +00:00
|
|
|
return ScopeUndefined, fmt.Errorf("%w: %s", ErrScopeUnknown, uid)
|
2021-12-25 18:55:59 +00:00
|
|
|
}
|
|
|
|
|
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 {
|
2022-01-29 17:50:45 +00:00
|
|
|
return fmt.Errorf("UnmarshalForm: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
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 {
|
2022-01-29 17:50:45 +00:00
|
|
|
return fmt.Errorf("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
|
|
|
|
2022-02-01 17:27:48 +00:00
|
|
|
for _, rawScope := range strings.Fields(src) {
|
|
|
|
scope, err := ParseScope(rawScope)
|
2021-12-29 20:08:30 +00:00
|
|
|
if err != nil {
|
2022-01-29 17:50:45 +00:00
|
|
|
return fmt.Errorf("UnmarshalJSON: %w", err)
|
|
|
|
}
|
|
|
|
|
2022-02-01 17:27:48 +00:00
|
|
|
if result.Has(scope) {
|
2022-01-29 17:50:45 +00:00
|
|
|
continue
|
2021-12-29 20:08:30 +00:00
|
|
|
}
|
|
|
|
|
2022-02-01 17:27:48 +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.
|
2022-01-08 10:50:49 +00:00
|
|
|
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 scope.
|
2021-12-25 18:55:59 +00:00
|
|
|
func (s Scope) String() string {
|
2022-01-29 17:50:45 +00:00
|
|
|
return s.uid
|
2021-12-25 18:55:59 +00:00
|
|
|
}
|
2021-12-29 20:08:30 +00:00
|
|
|
|
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] == ScopeUndefined {
|
|
|
|
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
|
|
|
|
}
|