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

feat: implement fetch jwk #1240

Merged
merged 9 commits into from
Feb 20, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
14 changes: 11 additions & 3 deletions x/zkauth/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
"net/http"
"os"
"path/filepath"
"strings"
"time"

"github.com/Finschia/finschia-sdk/codec"
Expand Down Expand Up @@ -45,8 +46,8 @@
go func() {
for {
select {
case <-ctx.Context().Done():
return

Check warning on line 50 in x/zkauth/keeper/keeper.go

View check run for this annotation

Codecov / codecov/patch

x/zkauth/keeper/keeper.go#L49-L50

Added lines #L49 - L50 were not covered by tests
default:
for _, name := range defaultZKAuthOAuthProviders {
provider := types.GetConfig(name)
Expand All @@ -54,24 +55,23 @@

resp, err := k.GetJWK(provider.JwkEndpoint)
if err != nil {
time.Sleep(time.Duration(fetchIntervals) * time.Second)
logger.Error(fmt.Sprintf("%s", err))
continue

Check warning on line 60 in x/zkauth/keeper/keeper.go

View check run for this annotation

Codecov / codecov/patch

x/zkauth/keeper/keeper.go#L58-L60

Added lines #L58 - L60 were not covered by tests
}

file, err := os.Create(filepath.Join(nodeHome, JwkFileName))
file, err := os.Create(filepath.Join(nodeHome, k.CreateJWKFileName(name)))
if err != nil {
time.Sleep(time.Duration(fetchIntervals) * time.Second)
logger.Error(fmt.Sprintf("%s", err))
fmt.Println(err)
continue

Check warning on line 67 in x/zkauth/keeper/keeper.go

View check run for this annotation

Codecov / codecov/patch

x/zkauth/keeper/keeper.go#L65-L67

Added lines #L65 - L67 were not covered by tests
}

_, err = io.Copy(file, resp.Body)
if err != nil {
time.Sleep(time.Duration(fetchIntervals) * time.Second)
logger.Error(fmt.Sprintf("%s", err))
continue

Check warning on line 74 in x/zkauth/keeper/keeper.go

View check run for this annotation

Codecov / codecov/patch

x/zkauth/keeper/keeper.go#L72-L74

Added lines #L72 - L74 were not covered by tests
}

resp.Body.Close()
Expand All @@ -85,19 +85,22 @@

func (k Keeper) ParseJWKs(byteArray []byte) (jwks []types.JWK, err error) {
var data map[string]interface{}
json.Unmarshal(byteArray, &data)
err = json.Unmarshal(byteArray, &data)
if err != nil {
return jwks, err
}

Check warning on line 91 in x/zkauth/keeper/keeper.go

View check run for this annotation

Codecov / codecov/patch

x/zkauth/keeper/keeper.go#L90-L91

Added lines #L90 - L91 were not covered by tests

for _, v := range data["keys"].([]interface{}) {
var jwk types.JWK

b, err := json.Marshal(v)
if err != nil {
return jwks, err
}

Check warning on line 99 in x/zkauth/keeper/keeper.go

View check run for this annotation

Codecov / codecov/patch

x/zkauth/keeper/keeper.go#L98-L99

Added lines #L98 - L99 were not covered by tests

if err := json.Unmarshal(b, &jwk); err != nil {
return jwks, err
}

Check warning on line 103 in x/zkauth/keeper/keeper.go

View check run for this annotation

Codecov / codecov/patch

x/zkauth/keeper/keeper.go#L102-L103

Added lines #L102 - L103 were not covered by tests

jwks = append(jwks, jwk)
}
Expand All @@ -108,13 +111,18 @@
func (k Keeper) GetJWK(endpoint string) (*http.Response, error) {
req, err := http.NewRequest("GET", endpoint, nil)
if err != nil {
return nil, err
}

Check warning on line 115 in x/zkauth/keeper/keeper.go

View check run for this annotation

Codecov / codecov/patch

x/zkauth/keeper/keeper.go#L114-L115

Added lines #L114 - L115 were not covered by tests
client := new(http.Client)
resp, err := client.Do(req)
if err != nil {
return nil, err
}

Check warning on line 120 in x/zkauth/keeper/keeper.go

View check run for this annotation

Codecov / codecov/patch

x/zkauth/keeper/keeper.go#L119-L120

Added lines #L119 - L120 were not covered by tests

return resp, nil
}

func (k Keeper) CreateJWKFileName(name types.OidcProvider) string {
fileNamePattern := strings.Replace(JwkFileName, ".", "-%s.", 1)
return fmt.Sprintf(fileNamePattern, name)
Mdaiki0730 marked this conversation as resolved.
Show resolved Hide resolved
}
11 changes: 5 additions & 6 deletions x/zkauth/keeper/keeper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,14 @@ package keeper_test
import (
"context"
"encoding/json"
"io/ioutil"
"io"
"net/http"
"net/http/httptest"
"os"
"path/filepath"
"testing"
"time"

"github.com/Finschia/finschia-sdk/x/zkauth/keeper"
"github.com/Finschia/finschia-sdk/x/zkauth/testutil"
"github.com/Finschia/finschia-sdk/x/zkauth/types"
"github.com/stretchr/testify/require"
Expand Down Expand Up @@ -63,7 +62,7 @@ func TestGetJWK(t *testing.T) {
require.NoError(t, err)

expected := testData
bodyBytes, err := ioutil.ReadAll(res.Body)
bodyBytes, err := io.ReadAll(res.Body)
bodyString := string(bodyBytes)
require.Equal(t, expected, bodyString)
}
Expand All @@ -77,7 +76,7 @@ func TestParseJWKs(t *testing.T) {
defer res.Body.Close()
require.NoError(t, err)

bodyBytes, err := ioutil.ReadAll(res.Body)
bodyBytes, err := io.ReadAll(res.Body)

jwks, err := k.ParseJWKs(bodyBytes)
require.NoError(t, err)
Expand All @@ -92,14 +91,14 @@ func TestFetchJwk(t *testing.T) {
timeoutCtx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()

tempDir, err := ioutil.TempDir("", types.StoreKey)
tempDir, err := os.MkdirTemp("", types.StoreKey)
require.NoError(t, err)
defer os.RemoveAll(tempDir)

k.FetchJWK(ctx.WithContext(timeoutCtx), tempDir)
<-timeoutCtx.Done()

content, err := ioutil.ReadFile(filepath.Join(tempDir, keeper.JwkFileName))
content, err := os.ReadFile(filepath.Join(tempDir, k.CreateJWKFileName(types.Google)))

require.NoError(t, err)
var expectedObj []types.JWK
Expand Down
Loading