2021-04-18 15:41:13 -04:00
|
|
|
// Copyright (c) 2015-2021 MinIO, Inc.
|
|
|
|
//
|
|
|
|
// This file is part of MinIO Object Storage stack
|
|
|
|
//
|
|
|
|
// This program is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU Affero General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
//
|
|
|
|
// This program is distributed in the hope that it will be useful
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU Affero General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU Affero General Public License
|
|
|
|
// along with this program. If not, see <http://www.gnu.org/licenses/>.
|
2020-08-10 16:30:12 -04:00
|
|
|
|
|
|
|
// 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
|
|
|
}
|