-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #93 from filecoin-project/feat/tanlang/add-local-a…
…uth-client feat: 增加本地的授权管理的包 / add local auth client
- Loading branch information
Showing
13 changed files
with
77 additions
and
5 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
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
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
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
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 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 | ||
} |
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 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) | ||
} |