2020-08-10 16:30:12 -04:00
|
|
|
/*
|
|
|
|
* MinIO Cloud Storage, (C) 2020 MinIO, Inc.
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
// Package licverifier implements a simple library to verify MinIO Subnet license keys.
|
|
|
|
package licverifier
|
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto/ecdsa"
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
2020-08-19 22:21:04 -04:00
|
|
|
"time"
|
2020-08-10 16:30:12 -04:00
|
|
|
|
|
|
|
"github.com/dgrijalva/jwt-go"
|
|
|
|
)
|
|
|
|
|
|
|
|
// LicenseVerifier needs an ECDSA public key in PEM format for initialization.
|
|
|
|
type LicenseVerifier struct {
|
|
|
|
ecPubKey *ecdsa.PublicKey
|
|
|
|
}
|
|
|
|
|
|
|
|
// LicenseInfo holds customer metadata present in the license key.
|
|
|
|
type LicenseInfo struct {
|
2020-08-19 22:21:04 -04:00
|
|
|
Email string // Email of the license key requestor
|
2021-01-06 13:17:05 -05:00
|
|
|
Organization string // Subnet organization name
|
2020-08-19 22:21:04 -04:00
|
|
|
AccountID int64 // Subnet account id
|
|
|
|
StorageCapacity int64 // Storage capacity used in TB
|
2021-01-06 13:17:05 -05:00
|
|
|
Plan string // Subnet plan
|
2020-08-19 22:21:04 -04:00
|
|
|
ExpiresAt time.Time // Time of license expiry
|
2020-08-10 16:30:12 -04:00
|
|
|
}
|
|
|
|
|
2020-08-13 00:31:52 -04:00
|
|
|
// license key JSON field names
|
|
|
|
const (
|
2021-01-06 13:17:05 -05:00
|
|
|
accountID = "aid"
|
|
|
|
sub = "sub"
|
|
|
|
expiresAt = "exp"
|
|
|
|
organization = "org"
|
|
|
|
capacity = "cap"
|
|
|
|
plan = "plan"
|
2020-08-13 00:31:52 -04:00
|
|
|
)
|
2020-08-10 16:30:12 -04:00
|
|
|
|
|
|
|
// NewLicenseVerifier returns an initialized license verifier with the given
|
|
|
|
// ECDSA public key in PEM format.
|
|
|
|
func NewLicenseVerifier(pemBytes []byte) (*LicenseVerifier, error) {
|
|
|
|
pbKey, err := jwt.ParseECPublicKeyFromPEM(pemBytes)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("Failed to parse public key: %s", err)
|
|
|
|
}
|
|
|
|
return &LicenseVerifier{
|
|
|
|
ecPubKey: pbKey,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2020-08-13 00:31:52 -04:00
|
|
|
// toLicenseInfo extracts LicenseInfo from claims. It returns an error if any of
|
|
|
|
// the claim values are invalid.
|
|
|
|
func toLicenseInfo(claims jwt.MapClaims) (LicenseInfo, error) {
|
|
|
|
accID, ok := claims[accountID].(float64)
|
2020-08-13 20:05:24 -04:00
|
|
|
if !ok || ok && accID <= 0 {
|
2020-08-13 00:31:52 -04:00
|
|
|
return LicenseInfo{}, errors.New("Invalid accountId in claims")
|
|
|
|
}
|
|
|
|
email, ok := claims[sub].(string)
|
|
|
|
if !ok {
|
|
|
|
return LicenseInfo{}, errors.New("Invalid email in claims")
|
|
|
|
}
|
2020-08-19 22:21:04 -04:00
|
|
|
expiryTS, ok := claims[expiresAt].(float64)
|
|
|
|
if !ok {
|
|
|
|
return LicenseInfo{}, errors.New("Invalid time of expiry in claims")
|
|
|
|
}
|
|
|
|
expiresAt := time.Unix(int64(expiryTS), 0)
|
2021-01-06 13:17:05 -05:00
|
|
|
orgName, ok := claims[organization].(string)
|
2020-08-13 00:31:52 -04:00
|
|
|
if !ok {
|
2021-01-06 13:17:05 -05:00
|
|
|
return LicenseInfo{}, errors.New("Invalid organization in claims")
|
2020-08-13 00:31:52 -04:00
|
|
|
}
|
|
|
|
storageCap, ok := claims[capacity].(float64)
|
|
|
|
if !ok {
|
|
|
|
return LicenseInfo{}, errors.New("Invalid storage capacity in claims")
|
|
|
|
}
|
2021-01-06 13:17:05 -05:00
|
|
|
plan, ok := claims[plan].(string)
|
2020-08-13 00:31:52 -04:00
|
|
|
if !ok {
|
2021-01-06 13:17:05 -05:00
|
|
|
return LicenseInfo{}, errors.New("Invalid plan in claims")
|
2020-08-13 00:31:52 -04:00
|
|
|
}
|
|
|
|
return LicenseInfo{
|
|
|
|
Email: email,
|
2021-01-06 13:17:05 -05:00
|
|
|
Organization: orgName,
|
2020-08-13 00:31:52 -04:00
|
|
|
AccountID: int64(accID),
|
|
|
|
StorageCapacity: int64(storageCap),
|
2021-01-06 13:17:05 -05:00
|
|
|
Plan: plan,
|
2020-08-19 22:21:04 -04:00
|
|
|
ExpiresAt: expiresAt,
|
2020-08-13 00:31:52 -04:00
|
|
|
}, nil
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2020-08-10 16:30:12 -04:00
|
|
|
// Verify verifies the license key and validates the claims present in it.
|
|
|
|
func (lv *LicenseVerifier) Verify(license string) (LicenseInfo, error) {
|
2020-08-13 00:31:52 -04:00
|
|
|
token, err := jwt.ParseWithClaims(license, &jwt.MapClaims{}, func(token *jwt.Token) (interface{}, error) {
|
2020-08-10 16:30:12 -04:00
|
|
|
return lv.ecPubKey, nil
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return LicenseInfo{}, fmt.Errorf("Failed to verify license: %s", err)
|
|
|
|
}
|
2020-08-13 00:31:52 -04:00
|
|
|
if claims, ok := token.Claims.(*jwt.MapClaims); ok && token.Valid {
|
|
|
|
return toLicenseInfo(*claims)
|
|
|
|
}
|
|
|
|
return LicenseInfo{}, errors.New("Invalid claims found in license")
|
2020-08-10 16:30:12 -04:00
|
|
|
}
|