mirror of
https://github.com/openziti/zrok.git
synced 2024-11-29 19:43:26 +01:00
79 lines
1.8 KiB
Go
79 lines
1.8 KiB
Go
package controller
|
|
|
|
import (
|
|
"crypto/rand"
|
|
"crypto/sha512"
|
|
"crypto/x509"
|
|
"encoding/hex"
|
|
errors2 "github.com/go-openapi/errors"
|
|
"github.com/lithammer/shortuuid/v4"
|
|
"github.com/openziti-test-kitchen/zrok/rest_model_zrok"
|
|
"github.com/openziti/edge/rest_management_api_client"
|
|
"github.com/openziti/edge/rest_util"
|
|
"github.com/pkg/errors"
|
|
"net/http"
|
|
"strings"
|
|
)
|
|
|
|
func ZrokAuthenticate(token string) (*rest_model_zrok.Principal, error) {
|
|
tx, err := str.Begin()
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer func() { _ = tx.Rollback() }()
|
|
if a, err := str.FindAccountWithToken(token, tx); err == nil {
|
|
principal := rest_model_zrok.Principal{
|
|
ID: int64(a.Id),
|
|
Token: a.Token,
|
|
Email: a.Email,
|
|
}
|
|
return &principal, nil
|
|
} else {
|
|
return nil, errors2.New(401, "invalid api key")
|
|
}
|
|
}
|
|
|
|
func edgeClient(cfg *ZitiConfig) (*rest_management_api_client.ZitiEdgeManagement, error) {
|
|
caCerts, err := rest_util.GetControllerWellKnownCas(cfg.ApiEndpoint)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
caPool := x509.NewCertPool()
|
|
for _, ca := range caCerts {
|
|
caPool.AddCert(ca)
|
|
}
|
|
return rest_util.NewEdgeManagementClientWithUpdb(cfg.Username, cfg.Password, cfg.ApiEndpoint, caPool)
|
|
}
|
|
|
|
func createToken() string {
|
|
return shortuuid.New()
|
|
}
|
|
|
|
func createServiceName() (string, error) {
|
|
bytes := make([]byte, 4)
|
|
if _, err := rand.Read(bytes); err != nil {
|
|
return "", errors.Wrap(err, "error generating service name")
|
|
}
|
|
return hex.EncodeToString(bytes), nil
|
|
}
|
|
|
|
func hashPassword(raw string) string {
|
|
hash := sha512.New()
|
|
hash.Write([]byte(raw))
|
|
return hex.EncodeToString(hash.Sum(nil))
|
|
}
|
|
|
|
func realRemoteAddress(req *http.Request) string {
|
|
ip := strings.Split(req.RemoteAddr, ":")[0]
|
|
fwdAddress := req.Header.Get("X-Forwarded-For")
|
|
if fwdAddress != "" {
|
|
ip = fwdAddress
|
|
|
|
ips := strings.Split(fwdAddress, ", ")
|
|
if len(ips) > 1 {
|
|
ip = ips[0]
|
|
}
|
|
}
|
|
return ip
|
|
}
|