richer principal (#11)

This commit is contained in:
Michael Quigley 2022-07-28 12:12:50 -04:00
parent cc86776351
commit e6e487c07e
No known key found for this signature in database
GPG Key ID: 9B60314A9DD20A62
5 changed files with 72 additions and 27 deletions

View File

@ -6,7 +6,6 @@ import (
"encoding/json"
"fmt"
"github.com/go-openapi/runtime/middleware"
"github.com/openziti-test-kitchen/zrok/controller/store"
"github.com/openziti-test-kitchen/zrok/rest_model_zrok"
"github.com/openziti-test-kitchen/zrok/rest_server_zrok/operations/identity"
"github.com/openziti/edge/rest_management_api_client"
@ -19,28 +18,12 @@ import (
)
func enableHandler(_ identity.EnableParams, principal *rest_model_zrok.Principal) middleware.Responder {
tx, err := Str.Begin()
if err != nil {
logrus.Errorf("error starting transaction: %v", err)
return identity.NewCreateAccountInternalServerError().WithPayload(rest_model_zrok.ErrorMessage(err.Error()))
}
a, err := Str.FindAccountWithToken(string(*principal), tx)
if err != nil {
logrus.Errorf("error finding account: %v", err)
return identity.NewCreateAccountBadRequest().WithPayload(rest_model_zrok.ErrorMessage(err.Error()))
}
if a == nil {
logrus.Errorf("account not found: %v", err)
return identity.NewEnableNotFound()
}
logrus.Infof("found account '%v'", a.Username)
client, err := edgeClient()
if err != nil {
logrus.Errorf("error getting edge client: %v", err)
return identity.NewEnableInternalServerError().WithPayload(rest_model_zrok.ErrorMessage(err.Error()))
}
ident, err := createIdentity(a, client)
ident, err := createIdentity(principal.Username, client)
if err != nil {
logrus.Error(err)
return identity.NewEnableInternalServerError().WithPayload(rest_model_zrok.ErrorMessage(err.Error()))
@ -67,13 +50,13 @@ func enableHandler(_ identity.EnableParams, principal *rest_model_zrok.Principal
return resp
}
func createIdentity(a *store.Account, client *rest_management_api_client.ZitiEdgeManagement) (*identity_edge.CreateIdentityCreated, error) {
func createIdentity(username string, client *rest_management_api_client.ZitiEdgeManagement) (*identity_edge.CreateIdentityCreated, error) {
iIsAdmin := false
iId, err := randomId()
if err != nil {
return nil, err
}
name := fmt.Sprintf("%v-%v", a.Username, iId)
name := fmt.Sprintf("%v-%v", username, iId)
identityType := rest_model_edge.IdentityTypeUser
i := &rest_model_edge.IdentityCreate{
Enrollment: &rest_model_edge.IdentityCreateEnrollment{Ott: true},

View File

@ -19,7 +19,11 @@ func ZrokAuthenticate(token string) (*rest_model_zrok.Principal, error) {
return nil, err
}
if a, err := Str.FindAccountWithToken(token, tx); err == nil {
principal := rest_model_zrok.Principal(a.Token)
principal := rest_model_zrok.Principal{
ID: int64(a.Id),
Token: a.Token,
Username: a.Username,
}
return &principal, nil
} else {
return nil, errors2.New(401, "invalid api key")

View File

@ -9,19 +9,48 @@ import (
"context"
"github.com/go-openapi/strfmt"
"github.com/go-openapi/swag"
)
// Principal principal
//
// swagger:model principal
type Principal string
type Principal struct {
// id
ID int64 `json:"id,omitempty"`
// token
Token string `json:"token,omitempty"`
// username
Username string `json:"username,omitempty"`
}
// Validate validates this principal
func (m Principal) Validate(formats strfmt.Registry) error {
func (m *Principal) Validate(formats strfmt.Registry) error {
return nil
}
// ContextValidate validates this principal based on context it is used
func (m Principal) ContextValidate(ctx context.Context, formats strfmt.Registry) error {
func (m *Principal) ContextValidate(ctx context.Context, formats strfmt.Registry) error {
return nil
}
// MarshalBinary interface implementation
func (m *Principal) MarshalBinary() ([]byte, error) {
if m == nil {
return nil, nil
}
return swag.WriteJSON(m)
}
// UnmarshalBinary interface implementation
func (m *Principal) UnmarshalBinary(b []byte) error {
var res Principal
if err := swag.ReadJSON(b, &res); err != nil {
return err
}
*m = res
return nil
}

View File

@ -215,8 +215,19 @@ func init() {
"type": "string"
},
"principal": {
"type": "object",
"properties": {
"id": {
"type": "integer"
},
"token": {
"type": "string"
},
"username": {
"type": "string"
}
}
},
"tunnelRequest": {
"type": "object",
"properties": {
@ -454,8 +465,19 @@ func init() {
"type": "string"
},
"principal": {
"type": "object",
"properties": {
"id": {
"type": "integer"
},
"token": {
"type": "string"
},
"username": {
"type": "string"
}
}
},
"tunnelRequest": {
"type": "object",
"properties": {

View File

@ -123,6 +123,13 @@ definitions:
errorMessage:
type: string
principal:
type: object
properties:
id:
type: integer
username:
type: string
token:
type: string
tunnelRequest:
type: object