github.com/Axway/agent-sdk@v1.1.101/pkg/authz/oauth/scopes.go (about)

     1  package oauth
     2  
     3  import (
     4  	"encoding/json"
     5  	"strconv"
     6  	"strings"
     7  )
     8  
     9  // Scopes - type for serializing scopes in client representation
    10  type Scopes []string
    11  
    12  // MarshalJSON - serializes the scopes in array as space separated string
    13  func (s *Scopes) MarshalJSON() ([]byte, error) {
    14  	scope := strings.Join([]string(*s), " ")
    15  	return json.Marshal(scope)
    16  }
    17  
    18  // UnmarshalJSON - deserializes the scopes from space separated string to array
    19  func (s *Scopes) UnmarshalJSON(data []byte) error {
    20  	strScopes := string(data)
    21  	strScopes, _ = strconv.Unquote(strScopes)
    22  	scopes := strings.Split(strScopes, " ")
    23  
    24  	for _, scope := range scopes {
    25  		*s = append([]string(*s), scope)
    26  	}
    27  	return nil
    28  }