-
Notifications
You must be signed in to change notification settings - Fork 0
feat: add identity package with URI-based entity model #138
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,137 @@ | ||
| package identity | ||
|
|
||
| import ( | ||
| "errors" | ||
| "strings" | ||
| ) | ||
|
|
||
| const ( | ||
| // Scheme is the URI scheme for krypton identities. | ||
| Scheme = "kryptonid" | ||
|
|
||
| // invalidChars contains characters that are not allowed in domain names, | ||
| // field types, or field names. | ||
| invalidChars = "*/\t\n\r " | ||
| ) | ||
|
|
||
| var ( | ||
| // ErrInvalidScheme is returned when parsing a URI with wrong scheme. | ||
| ErrInvalidScheme = errors.New("invalid scheme") | ||
|
|
||
| // ErrEmptyDomain is returned when a domain is empty. | ||
| ErrEmptyDomain = errors.New("domain cannot be empty") | ||
|
|
||
| // ErrInvalidCharacters is returned when a domain, field type, or field name | ||
| // contains characters from invalidChars. | ||
| ErrInvalidCharacters = errors.New("contains invalid characters") | ||
|
|
||
| // ErrInvalidPath is returned when the path has an incomplete type/name pair. | ||
| ErrInvalidPath = errors.New("invalid path: must have even number of fields (type/name pairs)") | ||
|
|
||
| // ErrEmptyField is returned when a field type or name is empty. | ||
| ErrEmptyField = errors.New("field type and name must be non-empty") | ||
| ) | ||
|
|
||
| type ( | ||
| // URI is the string representation of a krypton identity. | ||
| URI string | ||
|
|
||
| // Domain identifies a trust boundary for identities. | ||
| Domain string | ||
|
|
||
| // Field represents a type/name pair in the identity path. | ||
| Field struct { | ||
| Type string | ||
| Name string | ||
| } | ||
|
|
||
| // Identity is a kryptonid:// URI representing any entity in Krypton. | ||
| Identity struct { | ||
| Domain Domain | ||
| Fields []Field | ||
| } | ||
| ) | ||
|
|
||
| // Validate validates that the domain is non-empty and does not contain invalid characters. | ||
| func (d Domain) Validate() error { | ||
| if d == "" { | ||
| return ErrEmptyDomain | ||
| } | ||
| if strings.ContainsAny(string(d), invalidChars) { | ||
| return ErrInvalidCharacters | ||
| } | ||
| return nil | ||
| } | ||
|
|
||
| // Validate validates that type and name are non-empty and do not contain invalid characters. | ||
| func (f Field) Validate() error { | ||
| for _, v := range []string{f.Type, f.Name} { | ||
| if v == "" { | ||
| return ErrEmptyField | ||
| } | ||
| if strings.ContainsAny(v, invalidChars) { | ||
| return ErrInvalidCharacters | ||
| } | ||
| } | ||
| return nil | ||
| } | ||
|
|
||
| // URI returns the kryptonid:// string representation. | ||
| func (id *Identity) URI() URI { | ||
| var b strings.Builder | ||
|
|
||
| b.WriteString(Scheme) | ||
| b.WriteString("://") | ||
| b.WriteString(string(id.Domain)) | ||
|
|
||
| for _, f := range id.Fields { | ||
| b.WriteByte('/') | ||
| b.WriteString(f.Type) | ||
| b.WriteByte('/') | ||
| b.WriteString(f.Name) | ||
| } | ||
|
|
||
| return URI(b.String()) | ||
| } | ||
|
|
||
| // Parse parses a kryptonid:// URI into an Identity. | ||
| // Returns an error if the scheme is wrong, domain is empty or invalid, | ||
| // or the path has an odd number of fields (incomplete type/name pair). | ||
| // | ||
| // Example URI: kryptonid://acme-corp/node/root | ||
| func Parse(uri URI) (Identity, error) { | ||
| after, found := strings.CutPrefix(string(uri), Scheme+"://") | ||
| if !found { | ||
| return Identity{}, ErrInvalidScheme | ||
| } | ||
|
|
||
| d, path, _ := strings.Cut(after, "/") | ||
| if err := Domain(d).Validate(); err != nil { | ||
| return Identity{}, err | ||
| } | ||
|
|
||
| if path == "" { | ||
| return Identity{ | ||
| Domain: Domain(d), | ||
| }, nil | ||
| } | ||
|
|
||
| parts := strings.Split(path, "/") | ||
| if len(parts)%2 != 0 { | ||
| return Identity{}, ErrInvalidPath | ||
| } | ||
|
|
||
| fields := make([]Field, 0, len(parts)/2) | ||
| for i := 0; i < len(parts); i += 2 { | ||
| f := Field{Type: parts[i], Name: parts[i+1]} | ||
| if err := f.Validate(); err != nil { | ||
| return Identity{}, err | ||
| } | ||
| fields = append(fields, f) | ||
| } | ||
|
|
||
| return Identity{ | ||
| Domain: Domain(d), | ||
| Fields: fields, | ||
| }, nil | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.