Skip to content

Commit

Permalink
Merge pull request #93 from filecoin-project/feat/tanlang/add-local-a…
Browse files Browse the repository at this point in the history
…uth-client

feat: 增加本地的授权管理的包 / add local auth client
  • Loading branch information
hunjixin authored Aug 9, 2022
2 parents 9044c4f + 6b7078a commit bd046cb
Show file tree
Hide file tree
Showing 13 changed files with 77 additions and 5 deletions.
2 changes: 1 addition & 1 deletion cli/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ package cli
import (
"path"

"github.com/filecoin-project/venus-auth/cmd/jwtclient"
"github.com/filecoin-project/venus-auth/config"
"github.com/filecoin-project/venus-auth/jwtclient"
"github.com/mitchellh/go-homedir"
"github.com/urfave/cli/v2"
"golang.org/x/xerrors"
Expand Down
2 changes: 1 addition & 1 deletion integrate_test/miner_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"testing"

"github.com/filecoin-project/venus-auth/auth"
"github.com/filecoin-project/venus-auth/cmd/jwtclient"
"github.com/filecoin-project/venus-auth/jwtclient"
"github.com/stretchr/testify/assert"
)

Expand Down
2 changes: 1 addition & 1 deletion integrate_test/ratelimit_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"testing"

"github.com/filecoin-project/venus-auth/auth"
"github.com/filecoin-project/venus-auth/cmd/jwtclient"
"github.com/filecoin-project/venus-auth/jwtclient"
"github.com/filecoin-project/venus-auth/storage"
"github.com/stretchr/testify/assert"
)
Expand Down
2 changes: 1 addition & 1 deletion integrate_test/token_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"context"
"testing"

"github.com/filecoin-project/venus-auth/cmd/jwtclient"
"github.com/filecoin-project/venus-auth/jwtclient"
"github.com/stretchr/testify/assert"
)

Expand Down
2 changes: 1 addition & 1 deletion integrate_test/user_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import (
"testing"

"github.com/filecoin-project/venus-auth/auth"
"github.com/filecoin-project/venus-auth/cmd/jwtclient"
"github.com/filecoin-project/venus-auth/core"
"github.com/filecoin-project/venus-auth/jwtclient"
"github.com/stretchr/testify/assert"
)

Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
36 changes: 36 additions & 0 deletions jwtclient/local_auth_client.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package jwtclient

import (
"context"

"github.com/filecoin-project/go-jsonrpc/auth"
venusauth "github.com/filecoin-project/venus-auth/auth"
"github.com/filecoin-project/venus-auth/core"
jwt3 "github.com/gbrlsnchs/jwt/v3"
)

type LocalAuthClient struct {
alg *jwt3.HMACSHA
}

func NewLocalAuthClient(secret []byte, payload venusauth.JWTPayload) (*LocalAuthClient, []byte, error) {
client := &LocalAuthClient{
alg: jwt3.NewHS256(secret),
}

token, err := jwt3.Sign(payload, client.alg)
return client, token, err
}

func (c *LocalAuthClient) Verify(ctx context.Context, token string) ([]auth.Permission, error) {
var payload venusauth.JWTPayload
_, err := jwt3.Verify([]byte(token), c.alg, &payload)
if err != nil {
return nil, err
}

jwtPerms := core.AdaptOldStrategy(payload.Perm)
perms := make([]auth.Permission, len(jwtPerms))
copy(perms, jwtPerms)
return perms, nil
}
36 changes: 36 additions & 0 deletions jwtclient/local_auth_client_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package jwtclient

import (
"context"
"testing"

venusauth "github.com/filecoin-project/venus-auth/auth"
"github.com/filecoin-project/venus-auth/config"
"github.com/filecoin-project/venus-auth/core"
"github.com/stretchr/testify/assert"
)

func TestLocalAuthClient(t *testing.T) {
secret, err := config.RandSecret()
assert.NoError(t, err)

payload := venusauth.JWTPayload{
Perm: core.PermAdmin,
Name: "MarketLocalToken",
}

client, token, err := NewLocalAuthClient(secret, payload)
if err != nil {
t.Fatal(err)
}
ctx := context.Background()
permissions, err := client.Verify(ctx, string(token))
if err != nil {
t.Fatal(err)
}
assert.Equal(t, 4, len(permissions))
assert.Contains(t, permissions, core.PermAdmin)
assert.Contains(t, permissions, core.PermRead)
assert.Contains(t, permissions, core.PermWrite)
assert.Contains(t, permissions, core.PermSign)
}

0 comments on commit bd046cb

Please sign in to comment.