Last active
February 16, 2020 12:14
-
-
Save adxgun/8596e7774bc11ad3ef04b6de68e18490 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import ( | |
"github.com/docker/libtrust" | |
"crypto/tls" | |
"crypto/x509" | |
) | |
type tokenServer struct { | |
privateKey libtrust.PrivateKey | |
pubKey libtrust.PublicKey | |
crt, key string | |
} | |
// newTokenServer creates a new tokenServer | |
func newTokenServer(crt, key string) (*tokenServer, error) { | |
pk, prk, err := loadCertAndKey(crt, key) | |
if err != nil { | |
return nil, err | |
} | |
t := &tokenServer{privateKey: prk, pubKey: pk, crt: crt, key: key} | |
return t, nil | |
} | |
// loadCertAndKey from filesystem | |
func loadCertAndKey(certFile, keyFile string) (libtrust.PublicKey, libtrust.PrivateKey, error) { | |
cert, err := tls.LoadX509KeyPair(certFile, keyFile) | |
if err != nil { | |
return nil, nil, err | |
} | |
x509Cert, err := x509.ParseCertificate(cert.Certificate[0]) | |
if err != nil { | |
return nil, nil, err | |
} | |
pk, err := libtrust.FromCryptoPublicKey(x509Cert.PublicKey) | |
if err != nil { | |
return nil, nil, err | |
} | |
prk, err := libtrust.FromCryptoPrivateKey(cert.PrivateKey) | |
if err != nil { | |
return nil, nil, err | |
} | |
return pk, prk, nil | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment