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"
|
2022-07-28 18:07:37 +00:00
|
|
|
|
|
|
|
"source.toby3d.me/toby3d/auth/internal/common"
|
2021-12-25 18:55:59 +00:00
|
|
|
)
|
|
|
|
|
2022-12-26 16:23:57 +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
|
|
|
|
type Scope struct {
|
|
|
|
uid string
|
|
|
|
}
|
2021-12-25 18:55:59 +00:00
|
|
|
|
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-12-26 14:09:34 +00:00
|
|
|
//nolint:gochecknoglobals // structs cannot be constants
|
2021-12-25 18:55:59 +00:00
|
|
|
var (
|
2022-07-28 18:07:37 +00:00
|
|
|
ScopeUnd = Scope{uid: ""} // "und"
|
2021-12-25 18:55:59 +00:00
|
|
|
|
|
|
|
// https://indieweb.org/scope#Micropub_Scopes
|
2022-07-28 18:07:37 +00:00
|
|
|
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
|
2022-07-28 18:07:37 +00:00
|
|
|
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
|
2022-07-28 18:07:37 +00:00
|
|
|
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
|
2022-07-28 18:07:37 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2022-07-28 18:07:37 +00:00
|
|
|
return ScopeUnd, fmt.Errorf("%w: %s", ErrScopeUnknown, uid)
|
2021-12-25 18:55:59 +00:00
|
|
|
}
|
|
|
|
|
2022-02-15 20:07:39 +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 {
|
2022-07-28 18:07:37 +00:00
|
|
|
if s.uid != "" {
|
|
|
|
return s.uid
|
|
|
|
}
|
|
|
|
|
|
|
|
return common.Und
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s Scope) GoString() string {
|
|
|
|
return "domain.Scope(" + s.String() + ")"
|
2022-02-15 20:07:39 +00:00
|
|
|
}
|