| 1 | 6 | package At::Protocol::DID v1.8.0 { | |||||||||
| 2 | 6 | ▶ | use v5.42; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 3 | 6 | ▶ | no warnings qw[experimental::try]; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 4 | 6 | ▶ | use feature 'try'; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 5 | 6 | ▶ | use At::Error qw[register throw]; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 6 | 6 | ▶ | use parent -norequire => 'Exporter'; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 7 | use overload | ||||||||||
| 8 | 6 | ▶ | '""' => sub ( $s, $u, $q ) { | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.2
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||
| 9 | 0 | $$s; | |||||||||
| 10 | 6 | }; | |||||||||
| 11 | 6 | our %EXPORT_TAGS = ( all => [ our @EXPORT_OK = qw[ensureValidDid ensureValidDidRegex] ] ); | |||||||||
| 12 | |||||||||||
| 13 | 4 | ▶ | sub new( $class, $did ) { | ||||||||
|
Subroutine
new: called CC=1 SCAR=0.2
Pod
documented
| |||||||||||
| 14 | 4 | try { | |||||||||
| 15 | 4 | ensureValidDid($did); | |||||||||
| 16 | } | ||||||||||
| 17 | 0 | catch ($err) { return; } | |||||||||
| 18 | 4 | bless \$did, $class; | |||||||||
| 19 | } | ||||||||||
| 20 | |||||||||||
| 21 | #~ Taken from https://github.com/bluesky-social/atproto/blob/main/packages/syntax/src/did.ts | ||||||||||
| 22 | #~ Human-readable constraints: | ||||||||||
| 23 | #~ - valid W3C DID (https://www.w3.org/TR/did-core/#did-syntax) | ||||||||||
| 24 | #~ - entire URI is ASCII: [a-zA-Z0-9._:%-] | ||||||||||
| 25 | #~ - always starts "did:" (lower-case) | ||||||||||
| 26 | #~ - method name is one or more lower-case letters, followed by ":" | ||||||||||
| 27 | #~ - remaining identifier can have any of the above chars, but can not end in ":" | ||||||||||
| 28 | #~ - it seems that a bunch of ":" can be included, and don't need spaces between | ||||||||||
| 29 | #~ - "%" is used only for "percent encoding" and must be followed by two hex characters (and thus can't end in "%") | ||||||||||
| 30 | #~ - query ("?") and fragment ("#") stuff is defined for "DID URIs", but not as part of identifier itself | ||||||||||
| 31 | #~ - "The current specification does not take a position on the maximum length of a DID" | ||||||||||
| 32 | #~ - in current atproto, only allowing did:plc and did:web. But not *forcing* this at lexicon layer | ||||||||||
| 33 | #~ - hard length limit of 8KBytes | ||||||||||
| 34 | #~ - not going to validate "percent encoding" here | ||||||||||
| 35 | 213 | ▶ | sub ensureValidDid ($did) { | ||||||||
|
Subroutine
ensureValidDid: called CC=7 SCAR=19.5
Pod
documented
| |||||||||||
| 36 | |||||||||||
| 37 | # check that all chars are boring ASCII | ||||||||||
| 38 | 213 | ▶ | throw InvalidDidError('Disallowed characters in DID (ASCII letters, digits, and a couple other characters only)') | ||||||||
|
Branch: 1 branch
| |||||||||||
| 39 | unless $did =~ /^[a-zA-Z0-9._:%-]*$/; | ||||||||||
| 40 | # | ||||||||||
| 41 | 204 | my @parts = split ':', $did, -1; # negative limit, ftw | |||||||||
| 42 | 204 | ▶ | throw InvalidDidError('DID requires prefix, method, and method-specific content') if @parts < 3; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 43 | # | ||||||||||
| 44 | 195 | ▶ | throw InvalidDidError('DID requires "did:" prefix') if $parts[0] ne 'did'; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 45 | # | ||||||||||
| 46 | 189 | ▶ | throw InvalidDidError('DID method must be lower-case letters') if $parts[1] !~ /^[a-z]+$/; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 47 | # | ||||||||||
| 48 | 185 | ▶ | throw InvalidDidError('DID can not end with ":" or "%"') if $did =~ /[:%]$/; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 49 | 173 | ▶ | throw InvalidDidError('DID is too long (2048 characters max)') if length $did > 2 * 1024; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 50 | 171 | 1; | |||||||||
| 51 | } | ||||||||||
| 52 | |||||||||||
| 53 | 187 | ▶ | sub ensureValidDidRegex ($did) { | ||||||||
|
Subroutine
ensureValidDidRegex: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||
| 54 | |||||||||||
| 55 | #~ simple regex to enforce most constraints via just regex and length. | ||||||||||
| 56 | #~ hand wrote this regex based on above constraints | ||||||||||
| 57 | 187 | ▶ | throw InvalidDidError(q[DID didn't validate via regex]) if $did !~ /^did:[a-z]+:[a-zA-Z0-9._:%-]*[a-zA-Z0-9._-]$/; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 58 | 135 | ▶ | throw InvalidDidError('DID is too long (2048 characters max)') if length $did > 2 * 1024; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 59 | # | ||||||||||
| 60 | 133 | 1; | |||||||||
| 61 | } | ||||||||||
| 62 | |||||||||||
| 63 | # fatal error | ||||||||||
| 64 | 6 | register 'InvalidDidError', 1; | |||||||||
| 65 | }; | ||||||||||
| 66 | 6 | 1; | |||||||||
| 67 | __END__ | ||||||||||