Skip to content
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

fix: base64 schemas endpoint #1918

Merged
merged 1 commit into from
Nov 2, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions schema/handler.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
package schema

import (
"encoding/base64"
"encoding/json"
"fmt"
"io"
"io/ioutil"
"net/http"
"os"
"strings"

"github.com/ory/kratos/driver/config"
"github.com/ory/x/urlx"
Expand Down Expand Up @@ -193,6 +195,12 @@ func ReadSchema(schema *Schema) (src io.ReadCloser, err error) {
if err != nil {
return nil, errors.WithStack(err)
}
} else if schema.URL.Scheme == "base64" {
data, err := base64.StdEncoding.DecodeString(strings.TrimPrefix(schema.RawURL, "base64://"))
if err != nil {
return nil, errors.WithStack(err)
}
src = io.NopCloser(strings.NewReader(string(data)))
} else {
resp, err := http.Get(schema.URL.String())
if err != nil {
Expand Down
27 changes: 24 additions & 3 deletions schema/handler_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package schema_test

import (
"encoding/base64"
"encoding/json"
"fmt"
"io/ioutil"
Expand Down Expand Up @@ -40,6 +41,11 @@ func TestHandler(t *testing.T) {
URL: urlx.ParseOrPanic("file://./stub/identity-2.schema.json"),
RawURL: "file://./stub/identity-2.schema.json",
},
{
ID: "base64",
URL: urlx.ParseOrPanic("base64://ewogICIkc2NoZW1hIjogImh0dHA6Ly9qc29uLXNjaGVtYS5vcmcvZHJhZnQtMDcvc2NoZW1hIyIsCiAgInR5cGUiOiAib2JqZWN0IiwKICAicHJvcGVydGllcyI6IHsKICAgICJiYXIiOiB7CiAgICAgICJ0eXBlIjogInN0cmluZyIKICAgIH0KICB9LAogICJyZXF1aXJlZCI6IFsKICAgICJiYXIiCiAgXQp9"),
RawURL: "base64://ewogICIkc2NoZW1hIjogImh0dHA6Ly9qc29uLXNjaGVtYS5vcmcvZHJhZnQtMDcvc2NoZW1hIyIsCiAgInR5cGUiOiAib2JqZWN0IiwKICAicHJvcGVydGllcyI6IHsKICAgICJiYXIiOiB7CiAgICAgICJ0eXBlIjogInN0cmluZyIKICAgIH0KICB9LAogICJyZXF1aXJlZCI6IFsKICAgICJiYXIiCiAgXQp9",
},
{
ID: "unreachable",
URL: urlx.ParseOrPanic("http://127.0.0.1:12345/unreachable-schema"),
Expand Down Expand Up @@ -84,9 +90,18 @@ func TestHandler(t *testing.T) {
}

getFromFS := func(id string) []byte {
raw, err := os.ReadFile(strings.TrimPrefix(getSchemaById(id).RawURL, "file://"))
require.NoError(t, err)
return raw
schema := getSchemaById(id)

if schema.URL.Scheme == "file" {
raw, err := os.ReadFile(strings.TrimPrefix(schema.RawURL, "file://"))
require.NoError(t, err)
return raw
} else if schema.URL.Scheme == "base64" {
data, err := base64.StdEncoding.DecodeString(strings.TrimPrefix(schema.RawURL, "base64://"))
require.NoError(t, err)
return data
}
return nil
}

setSchemas := func(newSchemas schema.Schemas) {
Expand Down Expand Up @@ -119,6 +134,12 @@ func TestHandler(t *testing.T) {
require.JSONEq(t, string(file), string(server))
})

t.Run("case=get base64 schema", func(t *testing.T) {
server := getFromTSById("base64", http.StatusOK)
file := getFromFS("base64")
require.JSONEq(t, string(file), string(server))
})

t.Run("case=get unreachable schema", func(t *testing.T) {
reason := getFromTSById("unreachable", http.StatusInternalServerError)
require.Contains(t, string(reason), "could not be found or opened")
Expand Down