mirror of
https://github.com/openziti/zrok.git
synced 2024-12-23 15:18:52 +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) {
|
func CreateShare(root env_core.Root, request *ShareRequest) (*Share, error) {
|
||||||
|
var err error
|
||||||
|
var out *share.ShareParams
|
||||||
|
|
||||||
switch request.ShareMode {
|
switch request.ShareMode {
|
||||||
case PrivateShareMode:
|
case PrivateShareMode:
|
||||||
return newPrivateShare(root, request)
|
out = newPrivateShare(root, request)
|
||||||
case PublicShareMode:
|
case PublicShareMode:
|
||||||
return newPublicShare(root, request)
|
out = newPublicShare(root, request)
|
||||||
default:
|
default:
|
||||||
return nil, errors.Errorf("unknown share mode '%v'", request.ShareMode)
|
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 := share.NewShareParams()
|
||||||
req.Body = &rest_model_zrok.ShareRequest{
|
req.Body = &rest_model_zrok.ShareRequest{
|
||||||
EnvZID: root.Environment().ZitiIdentity,
|
EnvZID: root.Environment().ZitiIdentity,
|
||||||
@ -33,30 +61,10 @@ func newPrivateShare(root env_core.Root, request *ShareRequest) (*Share, error)
|
|||||||
BackendProxyEndpoint: request.Target,
|
BackendProxyEndpoint: request.Target,
|
||||||
AuthScheme: string(None),
|
AuthScheme: string(None),
|
||||||
}
|
}
|
||||||
if len(request.Auth) > 0 {
|
return req
|
||||||
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
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func newPublicShare(root env_core.Root, request *ShareRequest) (*Share, error) {
|
func newPublicShare(root env_core.Root, request *ShareRequest) *share.ShareParams {
|
||||||
req := share.NewShareParams()
|
req := share.NewShareParams()
|
||||||
req.Body = &rest_model_zrok.ShareRequest{
|
req.Body = &rest_model_zrok.ShareRequest{
|
||||||
EnvZID: root.Environment().ZitiIdentity,
|
EnvZID: root.Environment().ZitiIdentity,
|
||||||
@ -66,27 +74,7 @@ func newPublicShare(root env_core.Root, request *ShareRequest) (*Share, error) {
|
|||||||
BackendProxyEndpoint: request.Target,
|
BackendProxyEndpoint: request.Target,
|
||||||
AuthScheme: string(None),
|
AuthScheme: string(None),
|
||||||
}
|
}
|
||||||
if len(request.Auth) > 0 {
|
return req
|
||||||
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
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func DeleteShare(root env_core.Root, shrToken string) error {
|
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,
|
EnvZID: root.Environment().ZitiIdentity,
|
||||||
ShrToken: shrToken,
|
ShrToken: shrToken,
|
||||||
}
|
}
|
||||||
|
|
||||||
zrok, err := root.Client()
|
zrok, err := root.Client()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return errors.Wrap(err, "error getting zrok client")
|
return errors.Wrap(err, "error getting zrok client")
|
||||||
}
|
}
|
||||||
auth := httptransport.APIKeyAuth("X-TOKEN", "header", root.Environment().Token)
|
auth := httptransport.APIKeyAuth("X-TOKEN", "header", root.Environment().Token)
|
||||||
|
|
||||||
_, err = zrok.Share.Unshare(req, auth)
|
_, err = zrok.Share.Unshare(req, auth)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return errors.Wrap(err, "error deleting share")
|
return errors.Wrap(err, "error deleting share")
|
||||||
|
Loading…
Reference in New Issue
Block a user