-
Notifications
You must be signed in to change notification settings - Fork 88
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Credential VC Id is an URN and not a url (#680)
* feat: Return credential ID as URN * chore: linter * chore: Clearest implementation of urn package.
- Loading branch information
Showing
3 changed files
with
96 additions
and
10 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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,50 @@ | ||
package urn | ||
|
||
import ( | ||
"errors" | ||
|
||
"github.com/google/uuid" | ||
) | ||
|
||
// URN represents a Uniform Resource Name. | ||
type URN string | ||
|
||
// FromUUID creates a URN from a UUID. | ||
func FromUUID(uuid uuid.UUID) URN { | ||
return URN("urn:uuid:" + uuid.String()) | ||
} | ||
|
||
// UUID returns the UUID from a URN. It can throw an error to prevent bad constructor calls or urns without uuids | ||
func (u URN) UUID() (uuid.UUID, error) { | ||
if err := u.valid(); err != nil { | ||
return uuid.Nil, err | ||
} | ||
return uuid.Parse(string(u[9:])) | ||
} | ||
|
||
func (u URN) valid() error { | ||
if len(u) < len("urn:uuid:") { | ||
return errors.New("invalid uuid URN length") | ||
} | ||
if u[:9] != "urn:uuid:" { | ||
return errors.New("invalid uuid URN prefix") | ||
} | ||
return nil | ||
} | ||
|
||
// Parse creates a URN from a string. | ||
func Parse(u string) (URN, error) { | ||
if err := URN(u).valid(); err != nil { | ||
return "", err | ||
} | ||
return URN(u), nil | ||
} | ||
|
||
// UUIDFromURNString returns the UUID from a URN string. | ||
func UUIDFromURNString(s string) (uuid.UUID, error) { | ||
urn, err := Parse(s) | ||
if err != nil { | ||
return uuid.Nil, err | ||
} | ||
return urn.UUID() | ||
} |
This file contains 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,36 @@ | ||
package urn | ||
|
||
import ( | ||
"errors" | ||
"testing" | ||
|
||
"github.com/google/uuid" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestFromUUID(t *testing.T) { | ||
id := uuid.New() | ||
urn := FromUUID(id) | ||
assert.Equal(t, "urn:uuid:"+id.String(), string(urn)) | ||
} | ||
|
||
func TestUUIDFromURNString(t *testing.T) { | ||
for _, ts := range []struct { | ||
urn string | ||
err error | ||
}{ | ||
{"", errors.New("invalid uuid URN length")}, | ||
{"urn:uuid:", errors.New("invalid UUID length: 0")}, | ||
{"urn:uuid:1234", errors.New("invalid UUID length: 4")}, | ||
{"urn:uuid:123e4567-e89b-12d3-a456-426614174000", nil}, | ||
} { | ||
t.Run(ts.urn, func(t *testing.T) { | ||
u, err := UUIDFromURNString(ts.urn) | ||
if err == nil { | ||
assert.Equal(t, ts.urn[9:], u.String()) | ||
} else { | ||
assert.Equal(t, ts.err.Error(), err.Error()) | ||
} | ||
}) | ||
} | ||
} |