dispatch/storage/user_cert.go

69 lines
1.2 KiB
Go
Raw Normal View History

2016-01-11 20:04:57 +00:00
package storage
import (
"crypto/tls"
"errors"
"io/ioutil"
"os"
)
var (
ErrInvalidCert = errors.New("Invalid certificate")
ErrCouldNotSaveCert = errors.New("Could not save certificate")
)
2016-01-11 23:32:40 +00:00
func (u *User) GetCertificate() *tls.Certificate {
u.lock.Lock()
cert := u.certificate
u.lock.Unlock()
return cert
}
2016-01-11 20:04:57 +00:00
func (u *User) SetCertificate(certPEM, keyPEM []byte) error {
cert, err := tls.X509KeyPair(certPEM, keyPEM)
if err != nil {
return ErrInvalidCert
}
u.lock.Lock()
2016-01-11 23:32:40 +00:00
u.certificate = &cert
2016-01-11 20:04:57 +00:00
u.lock.Unlock()
err = os.MkdirAll(Path.User(u.UUID), 0700)
if err != nil {
return ErrCouldNotSaveCert
}
err = ioutil.WriteFile(Path.Certificate(u.UUID), certPEM, 0600)
if err != nil {
return ErrCouldNotSaveCert
}
err = ioutil.WriteFile(Path.Key(u.UUID), keyPEM, 0600)
if err != nil {
return ErrCouldNotSaveCert
}
return nil
}
func (u *User) loadCertificate() error {
certPEM, err := ioutil.ReadFile(Path.Certificate(u.UUID))
if err != nil {
return err
}
keyPEM, err := ioutil.ReadFile(Path.Key(u.UUID))
if err != nil {
return err
}
cert, err := tls.X509KeyPair(certPEM, keyPEM)
if err != nil {
return err
}
2016-01-11 23:32:40 +00:00
u.certificate = &cert
2016-01-11 20:04:57 +00:00
return nil
}