2022-02-11 22:48:35 +00:00
|
|
|
// Copyright (C) 2020 Storj Labs, Inc.
|
|
|
|
// See LICENSE for copying information.
|
|
|
|
|
2022-04-12 17:59:07 +01:00
|
|
|
package restkeys_test
|
2022-02-11 22:48:35 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"database/sql"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
|
|
|
|
"storj.io/common/testcontext"
|
|
|
|
"storj.io/common/testrand"
|
|
|
|
"storj.io/storj/private/testplanet"
|
2022-04-12 17:59:07 +01:00
|
|
|
"storj.io/storj/satellite/console/restkeys"
|
2022-02-11 22:48:35 +00:00
|
|
|
"storj.io/storj/satellite/oidc"
|
|
|
|
)
|
|
|
|
|
2022-04-12 17:59:07 +01:00
|
|
|
func TestRESTKeys(t *testing.T) {
|
2022-02-11 22:48:35 +00:00
|
|
|
testplanet.Run(t, testplanet.Config{
|
|
|
|
SatelliteCount: 1, StorageNodeCount: 0, UplinkCount: 1,
|
|
|
|
}, func(t *testing.T, ctx *testcontext.Context, planet *testplanet.Planet) {
|
|
|
|
sat := planet.Satellites[0]
|
2022-04-12 17:59:07 +01:00
|
|
|
service := sat.API.REST.Keys
|
2022-02-11 22:48:35 +00:00
|
|
|
|
|
|
|
id := testrand.UUID()
|
2022-03-27 11:16:46 +01:00
|
|
|
now := time.Now()
|
2022-02-11 22:48:35 +00:00
|
|
|
expires := time.Hour
|
|
|
|
apiKey, _, err := service.Create(ctx, id, expires)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
// test GetUserFromKey
|
2022-03-27 11:16:46 +01:00
|
|
|
userID, exp, err := service.GetUserAndExpirationFromKey(ctx, apiKey)
|
2022-02-11 22:48:35 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, id, userID)
|
2022-03-27 11:16:46 +01:00
|
|
|
require.False(t, exp.IsZero())
|
|
|
|
require.False(t, exp.Before(now))
|
2022-02-11 22:48:35 +00:00
|
|
|
|
|
|
|
// make sure an error is returned from duplicate apikey
|
|
|
|
hash, err := service.HashKey(ctx, apiKey)
|
|
|
|
require.NoError(t, err)
|
|
|
|
_, err = service.InsertIntoDB(ctx, oidc.OAuthToken{
|
|
|
|
UserID: id,
|
2022-04-12 17:59:07 +01:00
|
|
|
Kind: oidc.KindRESTTokenV0,
|
2022-02-11 22:48:35 +00:00
|
|
|
Token: hash,
|
|
|
|
}, now, expires)
|
2022-04-12 17:59:07 +01:00
|
|
|
require.True(t, restkeys.ErrDuplicateKey.Has(err))
|
2022-02-11 22:48:35 +00:00
|
|
|
|
|
|
|
// test revocation
|
|
|
|
require.NoError(t, service.Revoke(ctx, apiKey))
|
2022-04-12 17:59:07 +01:00
|
|
|
token, err := sat.DB.OIDC().OAuthTokens().Get(ctx, oidc.KindRESTTokenV0, hash)
|
2022-02-11 22:48:35 +00:00
|
|
|
require.Equal(t, sql.ErrNoRows, err)
|
|
|
|
require.True(t, token.ExpiresAt.IsZero())
|
|
|
|
|
|
|
|
// test revoke non existent key
|
|
|
|
nonexistent := testrand.UUID().String()
|
|
|
|
err = service.Revoke(ctx, nonexistent)
|
|
|
|
require.Error(t, err)
|
|
|
|
|
|
|
|
// test GetUserFromKey non existent key
|
2022-03-27 11:16:46 +01:00
|
|
|
_, _, err = service.GetUserAndExpirationFromKey(ctx, nonexistent)
|
2022-04-12 17:59:07 +01:00
|
|
|
require.True(t, restkeys.ErrInvalidKey.Has(err))
|
2022-02-11 22:48:35 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2022-04-12 17:59:07 +01:00
|
|
|
func TestRESTKeysExpiration(t *testing.T) {
|
2022-02-11 22:48:35 +00:00
|
|
|
testplanet.Run(t, testplanet.Config{
|
|
|
|
SatelliteCount: 1, StorageNodeCount: 0, UplinkCount: 1,
|
|
|
|
}, func(t *testing.T, ctx *testcontext.Context, planet *testplanet.Planet) {
|
|
|
|
sat := planet.Satellites[0]
|
2022-04-12 17:59:07 +01:00
|
|
|
service := sat.API.REST.Keys
|
2022-02-11 22:48:35 +00:00
|
|
|
now := time.Now()
|
|
|
|
|
|
|
|
// test no expiration uses default
|
|
|
|
expiresAt, err := service.InsertIntoDB(ctx, oidc.OAuthToken{
|
|
|
|
UserID: testrand.UUID(),
|
2022-04-12 17:59:07 +01:00
|
|
|
Kind: oidc.KindRESTTokenV0,
|
2022-02-11 22:48:35 +00:00
|
|
|
Token: "testhash0",
|
|
|
|
}, now, 0)
|
|
|
|
require.NoError(t, err)
|
2022-04-12 17:59:07 +01:00
|
|
|
require.Equal(t, now.Add(sat.Config.RESTKeys.DefaultExpiration), expiresAt)
|
2022-02-11 22:48:35 +00:00
|
|
|
|
|
|
|
// test negative expiration uses default
|
|
|
|
expiresAt, err = service.InsertIntoDB(ctx, oidc.OAuthToken{
|
|
|
|
UserID: testrand.UUID(),
|
2022-04-12 17:59:07 +01:00
|
|
|
Kind: oidc.KindRESTTokenV0,
|
2022-02-11 22:48:35 +00:00
|
|
|
Token: "testhash1",
|
|
|
|
}, now, -10000)
|
|
|
|
require.NoError(t, err)
|
2022-04-12 17:59:07 +01:00
|
|
|
require.Equal(t, now.Add(sat.Config.RESTKeys.DefaultExpiration), expiresAt)
|
2022-02-11 22:48:35 +00:00
|
|
|
|
|
|
|
// test regular expiration
|
|
|
|
expiration := 14 * time.Hour
|
|
|
|
expiresAt, err = service.InsertIntoDB(ctx, oidc.OAuthToken{
|
|
|
|
UserID: testrand.UUID(),
|
2022-04-12 17:59:07 +01:00
|
|
|
Kind: oidc.KindRESTTokenV0,
|
2022-02-11 22:48:35 +00:00
|
|
|
Token: "testhash2",
|
|
|
|
}, now, expiration)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, now.Add(expiration), expiresAt)
|
|
|
|
})
|
|
|
|
}
|