2019-03-01 00:23:06 +00:00
|
|
|
// Copyright 2019 Drone IO, 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.
|
2019-02-19 23:56:41 +00:00
|
|
|
|
|
|
|
// +build !nolimit
|
|
|
|
|
|
|
|
package license
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"encoding/json"
|
|
|
|
"io/ioutil"
|
|
|
|
"net/http"
|
|
|
|
|
|
|
|
"github.com/drone/drone/core"
|
|
|
|
"github.com/drone/go-license/license"
|
|
|
|
"github.com/drone/go-license/license/licenseutil"
|
|
|
|
)
|
|
|
|
|
|
|
|
// embedded public key used to verify license signatures.
|
|
|
|
var publicKey = []byte("GB/hFnXEg63vDZ2W6mKFhLxZTuxMrlN/C/0iVZ2LfPQ=")
|
|
|
|
|
|
|
|
// License renewal endpoint.
|
|
|
|
const licenseEndpoint = "https://license.drone.io/api/v1/license/renew"
|
|
|
|
|
|
|
|
// Trial returns a default license with trial terms based
|
|
|
|
// on the source code management system.
|
|
|
|
func Trial(provider string) *core.License {
|
|
|
|
switch provider {
|
|
|
|
case "gitea", "gogs":
|
|
|
|
return &core.License{
|
|
|
|
Kind: core.LicenseTrial,
|
|
|
|
Repos: 0,
|
|
|
|
Users: 0,
|
|
|
|
Builds: 0,
|
|
|
|
Nodes: 0,
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
return &core.License{
|
|
|
|
Kind: core.LicenseTrial,
|
|
|
|
Repos: 0,
|
|
|
|
Users: 0,
|
|
|
|
Builds: 15000,
|
|
|
|
Nodes: 0,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Load loads the license from file.
|
|
|
|
func Load(path string) (*core.License, error) {
|
|
|
|
pub, err := licenseutil.DecodePublicKey(publicKey)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
decoded, err := license.DecodeFile(path, pub)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if decoded.Expired() {
|
|
|
|
// if the license is expired we should check the license
|
|
|
|
// server to see if the license has been renewed. If yes
|
|
|
|
// we will load the renewed license.
|
|
|
|
|
|
|
|
buf := new(bytes.Buffer)
|
|
|
|
json.NewEncoder(buf).Encode(decoded)
|
|
|
|
res, err := http.Post(licenseEndpoint, "application/json", buf)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer res.Body.Close()
|
|
|
|
|
|
|
|
raw, err := ioutil.ReadAll(res.Body)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
decoded, err = license.Decode(raw, pub)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
license := new(core.License)
|
|
|
|
license.Expires = decoded.Exp
|
2019-03-13 21:47:47 +00:00
|
|
|
license.Licensor = decoded.Cus
|
|
|
|
license.Subscription = decoded.Sub
|
2019-02-19 23:56:41 +00:00
|
|
|
err = json.Unmarshal(decoded.Dat, license)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return license, err
|
|
|
|
}
|