subject

package
v0.1.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 30, 2023 License: MIT Imports: 6 Imported by: 0

README

Subject ID

Subject Identifiers for Security Event Tokens, implementing draft-ietf-secevent-subject-identifiers.

This is a nearly feature-complete implementation of draft-16, including the given examples as unit tests.

The code follows draft-16, which may be the Editor's copy rather than the published draft.

Usage

The library provides a simple api via the ID type as well as a set of constructors corresponding to each subject id format.

Given below is a simple example showing sample usage through the constructor:

package main

import (
    "github.com/bingxueshuang/gnap/subject"
)

func main() {
    // handle errors appropriately.
    isssub, _ := subject.NewIDIssSub("https://identity,example.org", "FNJ45HJ6")
    emailid, _ := subject.NewIDEmail("editor@example.org")
    userinfo, _ := subject.NewAliases([]subject.NoAlias{
        isssub.NoAlias(),
        emailid.NoAlias(),
    })
}

For more complicated use cases, directly use the ID type or the NoAlias type.

Validator method is given so as to allow a means to check if the subject id fields are valid or not.

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

Examples

Constants

This section is empty.

Variables

View Source
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.

View Source
var EmailRegex = regexp.MustCompile(`^([!#-'*+/-9=?A-Z^-~-]+(\.[!#-'*+/-9=?A-Z^-~-]+)*|"([]!#-[^-~ \t]|(\\[\t -~]))+")@([!#-'*+/-9=?A-Z^-~-]+(\.[!#-'*+/-9=?A-Z^-~-]+)*|\[[\t -Z^-~]*])$`)

EmailRegex according to [RFC5322].

View Source
var PhoneRegex = regexp.MustCompile(`^\+?[1-9]\d{1,14}$`)

PhoneRegex according to [E.164].

Functions

func Equal

func Equal(a, b ID) bool

Equal is simple equality comparison for ID.

func EqualNoAlias

func EqualNoAlias(a, b NoAlias) bool

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.

const (
	Account       Format = "account"
	Email         Format = "email"
	IssuerSubject Format = "iss_sub"
	Opaque        Format = "opaque"
	PhoneNumber   Format = "phone_number"
	DID           Format = "did"
	URI           Format = "uri"
	Aliases       Format = "aliases"
)

Subject identifier formats defined in the registery.

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

func NewIDAccount(acc string) (ID, error)

NewIDAccount creates a new subject identifier of format Account.

func NewIDAliases

func NewIDAliases(aliases []NoAlias) (ID, error)

NewIDAliases creates a new subject identifier of format Aliases.

func NewIDEmail

func NewIDEmail(email string) (ID, error)

NewIDEmail creates a new subject identifier of format Email.

func NewIDIssSub

func NewIDIssSub(iss string, sub string) (ID, error)

NewIDIssSub creates a new subject identifier of format IssuerSubject.

func NewIDOpaque

func NewIDOpaque(opaque string) (ID, error)

NewIDOpaque creates a new subject identifier of format Opaque.

func NewIDPhone

func NewIDPhone(phone string) (ID, error)

NewIDPhone creates a new subject identifier of format PhoneNumber.

func NewIDdid

func NewIDdid(did string) (ID, error)

NewIDdid creates a new subject identifier of format DID.

func NewIDuri

func NewIDuri(uri string) (ID, error)

NewIDuri creates a new subject identifier of format URI.

func (ID) NoAlias

func (id ID) NoAlias() (n NoAlias)

NoAlias is a helper to convert ID to NoAlias.

func (*ID) UnmarshalJSON

func (id *ID) UnmarshalJSON(data []byte) error

UnmarshalJSON implements the json.Unmarshaler interface.

func (ID) Validate

func (id ID) Validate() error

Validate checks if all fields of id are valid.

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) SubjectID

func (id NoAlias) SubjectID() (s ID)

SubjectID is a helper to convert NoAlias to ID.

func (*NoAlias) UnmarshalJSON

func (id *NoAlias) UnmarshalJSON(data []byte) error

UnmarshalJSON implements the json.Unmarshaler interface.

func (NoAlias) Validate

func (id NoAlias) Validate() error

Validate checks if all fields of id are valid.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL