Skip to content

Commit

Permalink
Merge pull request #199 from planetlabs/item-auth
Browse files Browse the repository at this point in the history
Support items with auth:schemes
  • Loading branch information
tschaub authored Sep 13, 2024
2 parents 2e49598 + 70afd7c commit 6932acc
Show file tree
Hide file tree
Showing 2 changed files with 189 additions and 0 deletions.
25 changes: 25 additions & 0 deletions extensions/auth/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,37 @@ const (
func init() {
r := regexp.MustCompile(extensionPattern)

stac.RegisterItemExtension(r, func() stac.Extension { return &Item{} })
stac.RegisterCollectionExtension(r, func() stac.Extension { return &Collection{} })
stac.RegisterCatalogExtension(r, func() stac.Extension { return &Catalog{} })
stac.RegisterAssetExtension(r, func() stac.Extension { return &Asset{} })
stac.RegisterLinkExtension(r, func() stac.Extension { return &Link{} })
}

type Item struct {
Schemes map[string]*Scheme `json:"auth:schemes,omitempty"`
}

var _ stac.Extension = (*Item)(nil)

func (*Item) URI() string {
return extensionUri
}

func (e *Item) Encode(itemMap map[string]any) error {
return stac.EncodeExtendedItemProperties(e, itemMap)
}

func (e *Item) Decode(itemMap map[string]any) error {
if err := stac.DecodeExtendedItemProperties(e, itemMap); err != nil {
return err
}
if e.Schemes == nil {
return stac.ErrExtensionDoesNotApply
}
return nil
}

type Collection struct {
Schemes map[string]*Scheme
}
Expand Down
164 changes: 164 additions & 0 deletions extensions/auth/auth_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,88 @@ import (
"github.com/stretchr/testify/require"
)

func TestItemExtendedMarshal(t *testing.T) {
item := &stac.Item{
Version: "1.0.0",
Id: "item-id",
Geometry: map[string]any{
"type": "Point",
"coordinates": []float64{0, 0},
},
Properties: map[string]any{
"test": "value",
},
Links: []*stac.Link{
{Href: "https://example.com/stac/item-id", Rel: "self"},
},
Assets: map[string]*stac.Asset{
"image": {
Title: "Image",
Href: "https://example.com/stac/item-id/image.tif",
Type: "image/tif",
Extensions: []stac.Extension{
&auth.Asset{
Refs: []string{"openid"},
},
},
},
},
Extensions: []stac.Extension{
&auth.Item{
Schemes: map[string]*auth.Scheme{
"openid": {
Type: "openIdConnect",
Description: "Test auth configuration",
OpenIdConnectUrl: "https://example.com/auth/.well-known/openid-configuration",
},
},
},
},
}

data, err := json.Marshal(item)
require.NoError(t, err)

expected := `{
"type": "Feature",
"stac_version": "1.0.0",
"id": "item-id",
"geometry": {
"type": "Point",
"coordinates": [0, 0]
},
"properties": {
"test": "value",
"auth:schemes": {
"openid": {
"type": "openIdConnect",
"description": "Test auth configuration",
"openIdConnectUrl": "https://example.com/auth/.well-known/openid-configuration"
}
}
},
"links": [
{
"rel": "self",
"href": "https://example.com/stac/item-id"
}
],
"assets": {
"image": {
"title": "Image",
"href": "https://example.com/stac/item-id/image.tif",
"type": "image/tif",
"auth:refs": ["openid"]
}
},
"stac_extensions": [
"https://stac-extensions.github.io/authentication/v1.1.0/schema.json"
]
}`

assert.JSONEq(t, expected, string(data))
}

func TestCollectionExtendedMarshal(t *testing.T) {
collection := &stac.Collection{
Version: "1.0.0",
Expand Down Expand Up @@ -150,6 +232,88 @@ func TestCollectionLinkExtendedMarshal(t *testing.T) {
assert.JSONEq(t, expected, string(data))
}

func TestItemExtendedUnmarshal(t *testing.T) {
data := []byte(`{
"type": "Feature",
"stac_version": "1.0.0",
"id": "item-id",
"geometry": {
"type": "Point",
"coordinates": [0, 0]
},
"properties": {
"test": "value",
"auth:schemes": {
"openid": {
"type": "openIdConnect",
"description": "Test auth configuration",
"openIdConnectUrl": "https://example.com/auth/.well-known/openid-configuration"
}
}
},
"links": [
{
"rel": "self",
"href": "https://example.com/stac/item-id"
}
],
"assets": {
"image": {
"title": "Image",
"href": "https://example.com/stac/item-id/image.tif",
"type": "image/tif",
"auth:refs": ["openid"]
}
},
"stac_extensions": [
"https://stac-extensions.github.io/authentication/v1.1.0/schema.json"
]
}`)

item := &stac.Item{}
require.NoError(t, json.Unmarshal(data, item))

expected := &stac.Item{
Version: "1.0.0",
Id: "item-id",
Geometry: map[string]any{
"type": "Point",
"coordinates": []any{float64(0), float64(0)},
},
Properties: map[string]any{
"test": "value",
},
Links: []*stac.Link{
{Href: "https://example.com/stac/item-id", Rel: "self"},
},
Assets: map[string]*stac.Asset{
"image": {
Title: "Image",
Href: "https://example.com/stac/item-id/image.tif",
Type: "image/tif",
Extensions: []stac.Extension{
&auth.Asset{
Refs: []string{"openid"},
},
},
},
},
Extensions: []stac.Extension{
&auth.Item{
Schemes: map[string]*auth.Scheme{
"openid": {
Type: "openIdConnect",
Description: "Test auth configuration",
OpenIdConnectUrl: "https://example.com/auth/.well-known/openid-configuration",
},
},
},
},
}

assert.Equal(t, expected, item)
}

func TestCollectionExtendedUnmarshal(t *testing.T) {
data := []byte(`{
"type": "Collection",
Expand Down

0 comments on commit 6932acc

Please sign in to comment.