Documentation ¶
Overview ¶
For creating a new Subject Identifier, use constructor functions matching:
NewID(format)?
where `format` is a subject id format. For more complicated usages, you can directly use ID as struct literal. For including list of identifiers in aliases format, ID.NoAlias helper function can be used. See the example of ID type for more details.
Example ¶
package main import ( "encoding/json" "fmt" "time" "github.com/bingxueshuang/gnap/subject" ) type SubInfo struct { SubIDs []subject.ID `json:"sub_ids"` UpdatedAt time.Time `json:"updated_at,omitempty"` } func main() { userinfo, _ := subject.NewIDOpaque("J2G8G8O4AZ") lastupdated, _ := time.Parse(time.RFC3339, "2006-01-02T15:04:05Z") user := SubInfo{ []subject.ID{userinfo}, lastupdated, } data, _ := json.MarshalIndent(user, "", " ") fmt.Println(string(data)) }
Output: { "sub_ids": [ { "format": "opaque", "id": "J2G8G8O4AZ" } ], "updated_at": "2006-01-02T15:04:05Z" }
Index ¶
- Variables
- func Equal(a, b ID) bool
- func EqualNoAlias(a, b NoAlias) bool
- type Format
- type ID
- func NewIDAccount(acc string) (ID, error)
- func NewIDAliases(aliases []NoAlias) (ID, error)
- func NewIDEmail(email string) (ID, error)
- func NewIDIssSub(iss string, sub string) (ID, error)
- func NewIDOpaque(opaque string) (ID, error)
- func NewIDPhone(phone string) (ID, error)
- func NewIDdid(did string) (ID, error)
- func NewIDuri(uri string) (ID, error)
- type NoAlias
Examples ¶
Constants ¶
This section is empty.
Variables ¶
var ( ErrInvalidFormat = errors.New("format not defined in the registry") ErrInvalidSubjectID = errors.New("malformed subject identifier") ErrInvalidAliases = errors.New("malformed subject id aliases") )
Errors during creating subject id object.
var EmailRegex = regexp.MustCompile(`^([!#-'*+/-9=?A-Z^-~-]+(\.[!#-'*+/-9=?A-Z^-~-]+)*|"([]!#-[^-~ \t]|(\\[\t -~]))+")@([!#-'*+/-9=?A-Z^-~-]+(\.[!#-'*+/-9=?A-Z^-~-]+)*|\[[\t -Z^-~]*])$`)
EmailRegex according to [RFC5322].
var PhoneRegex = regexp.MustCompile(`^\+?[1-9]\d{1,14}$`)
PhoneRegex according to [E.164].
Functions ¶
func EqualNoAlias ¶
EqualNoAlias is a simple equality comparison for NoAlias. Useful for comparing those in ID.Identifiers.
Types ¶
type Format ¶
type Format string
Format represents a subject identifier format as defined in Security Event Identifier Format Registry.
type ID ¶
type ID struct { Format Format `json:"format"` URI string `json:"uri,omitempty"` Email string `json:"email,omitempty"` Issuer string `json:"iss,omitempty"` Subject string `json:"sub,omitempty"` ID string `json:"id,omitempty"` Phone string `json:"phone_number,omitempty"` URL string `json:"url,omitempty"` Identifiers []NoAlias `json:"identifiers,omitempty"` }
ID is IETF Subject Identifier for Security Events.
Example ¶
package main import ( "encoding/json" "fmt" "github.com/bingxueshuang/gnap/subject" ) func main() { var testid subject.ID data := []byte(`{ "format": "aliases", "identifiers": [ { "format": "email", "email": "user@example.com" }, { "format": "phone_number", "phone_number": "+12065550100" }, { "format": "email", "email": "user+word@example.com" } ] }`) _ = json.Unmarshal(data, &testid) personal, _ := subject.NewIDEmail("user@example.com") number, _ := subject.NewIDPhone("+12065550100") workmail, _ := subject.NewIDEmail("user+word@example.com") newid, _ := subject.NewIDAliases([]subject.NoAlias{ personal.NoAlias(), number.NoAlias(), workmail.NoAlias(), }) fmt.Println(subject.Equal(newid, testid)) }
Output: true
func NewIDAccount ¶
NewIDAccount creates a new subject identifier of format Account.
func NewIDAliases ¶
NewIDAliases creates a new subject identifier of format Aliases.
func NewIDEmail ¶
NewIDEmail creates a new subject identifier of format Email.
func NewIDIssSub ¶
NewIDIssSub creates a new subject identifier of format IssuerSubject.
func NewIDOpaque ¶
NewIDOpaque creates a new subject identifier of format Opaque.
func NewIDPhone ¶
NewIDPhone creates a new subject identifier of format PhoneNumber.
func (*ID) UnmarshalJSON ¶
UnmarshalJSON implements the json.Unmarshaler interface.
type NoAlias ¶
type NoAlias struct { Format Format `json:"format"` URI string `json:"uri,omitempty"` Email string `json:"email,omitempty"` Issuer string `json:"iss,omitempty"` Subject string `json:"sub,omitempty"` ID string `json:"id,omitempty"` Phone string `json:"phone_number,omitempty"` URL string `json:"url,omitempty"` }
NoAlias is subject identifier with any valid format except aliases (to prevent nesting).
func (*NoAlias) UnmarshalJSON ¶
UnmarshalJSON implements the json.Unmarshaler interface.