mirror of
https://github.com/openziti/zrok.git
synced 2024-12-23 07:09:12 +01:00
slightly improved version of the generalized share implementation (#34)
This commit is contained in:
parent
c26d325f61
commit
7dc14445ad
82
sdk/share.go
82
sdk/share.go
@ -14,17 +14,45 @@ type Share struct {
|
||||
}
|
||||
|
||||
func CreateShare(root env_core.Root, request *ShareRequest) (*Share, error) {
|
||||
var err error
|
||||
var out *share.ShareParams
|
||||
|
||||
switch request.ShareMode {
|
||||
case PrivateShareMode:
|
||||
return newPrivateShare(root, request)
|
||||
out = newPrivateShare(root, request)
|
||||
case PublicShareMode:
|
||||
return newPublicShare(root, request)
|
||||
out = newPublicShare(root, request)
|
||||
default:
|
||||
return nil, errors.Errorf("unknown share mode '%v'", request.ShareMode)
|
||||
}
|
||||
|
||||
if len(request.Auth) > 0 {
|
||||
out.Body.AuthScheme = string(Basic)
|
||||
for _, pair := range request.Auth {
|
||||
tokens := strings.Split(pair, ":")
|
||||
if len(tokens) == 2 {
|
||||
out.Body.AuthUsers = append(out.Body.AuthUsers, &rest_model_zrok.AuthUser{Username: strings.TrimSpace(tokens[0]), Password: strings.TrimSpace(tokens[1])})
|
||||
} else {
|
||||
return nil, errors.Errorf("invalid username:password pair '%v'", pair)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
zrok, err := root.Client()
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "error getting zrok client")
|
||||
}
|
||||
auth := httptransport.APIKeyAuth("X-TOKEN", "header", root.Environment().Token)
|
||||
|
||||
in, err := zrok.Share.Share(out, auth)
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "unable to create share")
|
||||
}
|
||||
|
||||
return &Share{Token: in.Payload.ShrToken}, nil
|
||||
}
|
||||
|
||||
func newPrivateShare(root env_core.Root, request *ShareRequest) (*Share, error) {
|
||||
func newPrivateShare(root env_core.Root, request *ShareRequest) *share.ShareParams {
|
||||
req := share.NewShareParams()
|
||||
req.Body = &rest_model_zrok.ShareRequest{
|
||||
EnvZID: root.Environment().ZitiIdentity,
|
||||
@ -33,30 +61,10 @@ func newPrivateShare(root env_core.Root, request *ShareRequest) (*Share, error)
|
||||
BackendProxyEndpoint: request.Target,
|
||||
AuthScheme: string(None),
|
||||
}
|
||||
if len(request.Auth) > 0 {
|
||||
req.Body.AuthScheme = string(Basic)
|
||||
for _, pair := range request.Auth {
|
||||
tokens := strings.Split(pair, ":")
|
||||
if len(tokens) == 2 {
|
||||
req.Body.AuthUsers = append(req.Body.AuthUsers, &rest_model_zrok.AuthUser{Username: strings.TrimSpace(tokens[0]), Password: strings.TrimSpace(tokens[1])})
|
||||
} else {
|
||||
return nil, errors.Errorf("invalid username:password pair '%v'", pair)
|
||||
}
|
||||
}
|
||||
}
|
||||
zrok, err := root.Client()
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "error getting zrok client")
|
||||
}
|
||||
auth := httptransport.APIKeyAuth("X-TOKEN", "header", root.Environment().Token)
|
||||
resp, err := zrok.Share.Share(req, auth)
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "unable to create share")
|
||||
}
|
||||
return &Share{Token: resp.Payload.ShrToken}, nil
|
||||
return req
|
||||
}
|
||||
|
||||
func newPublicShare(root env_core.Root, request *ShareRequest) (*Share, error) {
|
||||
func newPublicShare(root env_core.Root, request *ShareRequest) *share.ShareParams {
|
||||
req := share.NewShareParams()
|
||||
req.Body = &rest_model_zrok.ShareRequest{
|
||||
EnvZID: root.Environment().ZitiIdentity,
|
||||
@ -66,27 +74,7 @@ func newPublicShare(root env_core.Root, request *ShareRequest) (*Share, error) {
|
||||
BackendProxyEndpoint: request.Target,
|
||||
AuthScheme: string(None),
|
||||
}
|
||||
if len(request.Auth) > 0 {
|
||||
req.Body.AuthScheme = string(Basic)
|
||||
for _, pair := range request.Auth {
|
||||
tokens := strings.Split(pair, ":")
|
||||
if len(tokens) == 2 {
|
||||
req.Body.AuthUsers = append(req.Body.AuthUsers, &rest_model_zrok.AuthUser{Username: strings.TrimSpace(tokens[0]), Password: strings.TrimSpace(tokens[1])})
|
||||
} else {
|
||||
return nil, errors.Errorf("invalid username:password pair '%v'", pair)
|
||||
}
|
||||
}
|
||||
}
|
||||
zrok, err := root.Client()
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "error getting zrok client")
|
||||
}
|
||||
auth := httptransport.APIKeyAuth("X-TOKEN", "header", root.Environment().Token)
|
||||
resp, err := zrok.Share.Share(req, auth)
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "unable to create share")
|
||||
}
|
||||
return &Share{Token: resp.Payload.ShrToken}, nil
|
||||
return req
|
||||
}
|
||||
|
||||
func DeleteShare(root env_core.Root, shrToken string) error {
|
||||
@ -95,11 +83,13 @@ func DeleteShare(root env_core.Root, shrToken string) error {
|
||||
EnvZID: root.Environment().ZitiIdentity,
|
||||
ShrToken: shrToken,
|
||||
}
|
||||
|
||||
zrok, err := root.Client()
|
||||
if err != nil {
|
||||
return errors.Wrap(err, "error getting zrok client")
|
||||
}
|
||||
auth := httptransport.APIKeyAuth("X-TOKEN", "header", root.Environment().Token)
|
||||
|
||||
_, err = zrok.Share.Unshare(req, auth)
|
||||
if err != nil {
|
||||
return errors.Wrap(err, "error deleting share")
|
||||
|
Loading…
Reference in New Issue
Block a user