Add ExpiresAt to LicenseInfo (#10293)

master
Krishnan Parthasarathi 4 years ago committed by GitHub
parent 0ebb73ee2e
commit ccd967e3be
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 19
      pkg/licverifier/verifier.go
  2. 10
      pkg/licverifier/verifier_test.go

@ -21,6 +21,7 @@ import (
"crypto/ecdsa"
"errors"
"fmt"
"time"
"github.com/dgrijalva/jwt-go"
)
@ -32,17 +33,19 @@ type LicenseVerifier struct {
// LicenseInfo holds customer metadata present in the license key.
type LicenseInfo struct {
Email string // Email of the license key requestor
TeamName string // Subnet team name
AccountID int64 // Subnet account id
StorageCapacity int64 // Storage capacity used in TB
ServiceType string // Subnet service type
Email string // Email of the license key requestor
TeamName string // Subnet team name
AccountID int64 // Subnet account id
StorageCapacity int64 // Storage capacity used in TB
ServiceType string // Subnet service type
ExpiresAt time.Time // Time of license expiry
}
// license key JSON field names
const (
accountID = "accountId"
sub = "sub"
expiresAt = "exp"
teamName = "teamName"
capacity = "capacity"
serviceType = "serviceType"
@ -71,6 +74,11 @@ func toLicenseInfo(claims jwt.MapClaims) (LicenseInfo, error) {
if !ok {
return LicenseInfo{}, errors.New("Invalid email in claims")
}
expiryTS, ok := claims[expiresAt].(float64)
if !ok {
return LicenseInfo{}, errors.New("Invalid time of expiry in claims")
}
expiresAt := time.Unix(int64(expiryTS), 0)
tName, ok := claims[teamName].(string)
if !ok {
return LicenseInfo{}, errors.New("Invalid team name in claims")
@ -89,6 +97,7 @@ func toLicenseInfo(claims jwt.MapClaims) (LicenseInfo, error) {
AccountID: int64(accID),
StorageCapacity: int64(storageCap),
ServiceType: sType,
ExpiresAt: expiresAt,
}, nil
}

@ -31,6 +31,13 @@ func at(t time.Time, f func()) {
jwt.TimeFunc = time.Now
}
func areEqLicenseInfo(a, b LicenseInfo) bool {
if a.Email == b.Email && a.TeamName == b.TeamName && a.AccountID == b.AccountID && a.ServiceType == b.ServiceType && a.StorageCapacity == b.StorageCapacity && a.ExpiresAt.Equal(b.ExpiresAt) {
return true
}
return false
}
// TestLicenseVerify tests the license key verification process with a valid and
// an invalid key.
func TestLicenseVerify(t *testing.T) {
@ -54,6 +61,7 @@ mr/cKCUyBL7rcAvg0zNq1vcSrUSGlAmY3SEDCu3GOKnjG/U4E7+p957ocWSV+mQU
AccountID: 1,
StorageCapacity: 50,
ServiceType: "STANDARD",
ExpiresAt: time.Date(2021, time.August, 5, 15, 17, 42, 0, time.FixedZone("PDT", -7*60*60)),
}, true},
}
@ -69,7 +77,7 @@ mr/cKCUyBL7rcAvg0zNq1vcSrUSGlAmY3SEDCu3GOKnjG/U4E7+p957ocWSV+mQU
if !tc.shouldPass {
t.Fatalf("%d: Expected license to fail verification but passed", i+1)
}
if tc.expectedLicInfo != licInfo {
if !areEqLicenseInfo(tc.expectedLicInfo, licInfo) {
t.Fatalf("%d: Expected license info %v but got %v", i+1, tc.expectedLicInfo, licInfo)
}
}

Loading…
Cancel
Save