-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
fe8f8fe
commit 83b0f41
Showing
7 changed files
with
220 additions
and
55 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package authcontrol | ||
|
||
import "fmt" | ||
|
||
var ( | ||
ErrEmptyJWTSecret error = fmt.Errorf("JWTSecret is empty") | ||
ErrS2SClientConfigIsNil error = fmt.Errorf("S2SClientConfig is 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
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,98 @@ | ||
package authcontrol_test | ||
|
||
import ( | ||
"context" | ||
"os" | ||
"testing" | ||
"time" | ||
|
||
"github.com/go-chi/jwtauth/v5" | ||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
|
||
"github.com/0xsequence/authcontrol" | ||
) | ||
|
||
func TestS2SClient(t *testing.T) { | ||
secret := "secret" | ||
serviceName := "test-service-name" | ||
|
||
cfg := &authcontrol.S2SClientConfig{ | ||
JWTSecret: secret, | ||
ServiceName: serviceName, | ||
Expiration: 10 * time.Second, | ||
} | ||
|
||
err := cfg.Validate() | ||
require.NoError(t, err) | ||
|
||
s2sClient, err := authcontrol.S2SClient(cfg) | ||
require.NoError(t, err) | ||
require.NotNil(t, s2sClient) | ||
|
||
s2sClient, err = authcontrol.S2SClient(nil) | ||
require.Error(t, err) | ||
require.ErrorIs(t, err, authcontrol.ErrS2SClientConfigIsNil) | ||
require.Nil(t, s2sClient) | ||
|
||
cfg = &authcontrol.S2SClientConfig{ | ||
JWTSecret: "", | ||
} | ||
s2sClient, err = authcontrol.S2SClient(cfg) | ||
require.Error(t, err) | ||
require.ErrorIs(t, err, authcontrol.ErrEmptyJWTSecret) | ||
require.Nil(t, s2sClient) | ||
|
||
cfg = &authcontrol.S2SClientConfig{ | ||
JWTSecret: "", | ||
} | ||
err = cfg.Validate() | ||
require.Error(t, err) | ||
require.ErrorIs(t, err, authcontrol.ErrEmptyJWTSecret) | ||
} | ||
|
||
func TestS2SToken(t *testing.T) { | ||
ctx := context.Background() | ||
secret := "secret" | ||
serviceName := "test-service-name" | ||
|
||
cfg := &authcontrol.S2STokenConfig{ | ||
JWTSecret: secret, | ||
ServiceName: serviceName, | ||
Expiration: 10 * time.Second, | ||
} | ||
|
||
err := cfg.Validate() | ||
require.NoError(t, err) | ||
|
||
jwtAut := jwtauth.New("HS256", []byte(secret), nil) | ||
jwtToken := authcontrol.S2SToken(cfg) | ||
|
||
token, err := jwtauth.VerifyToken(jwtAut, jwtToken) | ||
require.NoError(t, err) | ||
|
||
claims, err := token.AsMap(ctx) | ||
require.NoError(t, err) | ||
|
||
cServiceName := claims["service"].(string) | ||
assert.Equal(t, serviceName, cServiceName) | ||
|
||
cfg = &authcontrol.S2STokenConfig{ | ||
JWTSecret: secret, | ||
Expiration: 10 * time.Second, | ||
} | ||
|
||
jwtToken = authcontrol.S2SToken(cfg) | ||
|
||
token, err = jwtauth.VerifyToken(jwtAut, jwtToken) | ||
require.NoError(t, err) | ||
|
||
claims, err = token.AsMap(ctx) | ||
require.NoError(t, err) | ||
|
||
cServiceName = claims["service"].(string) | ||
assert.Equal(t, os.Args[0], cServiceName) | ||
|
||
jwtToken = authcontrol.S2SToken(nil) | ||
assert.Equal(t, "", jwtToken) | ||
} |
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
Oops, something went wrong.