2022-12-26 16:23:57 +00:00
|
|
|
package domain
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
2024-05-06 16:23:11 +00:00
|
|
|
|
|
|
|
"source.toby3d.me/toby3d/auth/internal/domain/scope"
|
2022-12-26 16:23:57 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// Scopes represent set of Scope domains.
|
2024-05-06 16:23:11 +00:00
|
|
|
type Scopes []scope.Scope
|
2022-12-26 16:23:57 +00:00
|
|
|
|
|
|
|
// UnmarshalForm implements custom unmarshler for form values.
|
|
|
|
func (s *Scopes) UnmarshalForm(v []byte) error {
|
|
|
|
for _, rawScope := range strings.Fields(string(v)) {
|
2024-05-06 16:23:11 +00:00
|
|
|
scope, err := scope.Parse(rawScope)
|
2022-12-26 16:23:57 +00:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Scopes: UnmarshalForm: %w", err)
|
|
|
|
}
|
|
|
|
|
2023-08-05 23:58:57 +00:00
|
|
|
if s.Has(scope) {
|
2022-12-26 16:23:57 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2023-08-05 23:58:57 +00:00
|
|
|
*s = append(*s, scope)
|
2022-12-26 16:23:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// UnmarshalJSON implements custom unmarshler for JSON.
|
|
|
|
func (s *Scopes) UnmarshalJSON(v []byte) error {
|
|
|
|
src, err := strconv.Unquote(string(v))
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Scopes: UnmarshalJSON: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, rawScope := range strings.Fields(src) {
|
2024-05-06 16:23:11 +00:00
|
|
|
scope, err := scope.Parse(rawScope)
|
2022-12-26 16:23:57 +00:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Scopes: UnmarshalJSON: %w", err)
|
|
|
|
}
|
|
|
|
|
2023-08-05 23:58:57 +00:00
|
|
|
if s.Has(scope) {
|
2022-12-26 16:23:57 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2023-08-05 23:58:57 +00:00
|
|
|
*s = append(*s, scope)
|
2022-12-26 16:23:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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
|
|
|
|
}
|
|
|
|
|
|
|
|
// String returns string representation of scopes.
|
|
|
|
func (s Scopes) String() string {
|
|
|
|
scopes := make([]string, len(s))
|
|
|
|
|
|
|
|
for i := range s {
|
|
|
|
scopes[i] = s[i].String()
|
|
|
|
}
|
|
|
|
|
|
|
|
return strings.Join(scopes, " ")
|
|
|
|
}
|
|
|
|
|
|
|
|
// IsEmpty returns true if the set does not contain valid scope.
|
|
|
|
func (s Scopes) IsEmpty() bool {
|
|
|
|
for i := range s {
|
2024-05-06 16:23:11 +00:00
|
|
|
if s[i] == scope.Und {
|
2022-12-26 16:23:57 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
// Has check what input scope contains in current scopes collection.
|
2024-05-06 16:23:11 +00:00
|
|
|
func (s Scopes) Has(scope scope.Scope) bool {
|
2022-12-26 16:23:57 +00:00
|
|
|
for i := range s {
|
|
|
|
if s[i] != scope {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
|
|
|
}
|