diff options
author | Nick Thomas <nick@gitlab.com> | 2019-10-17 12:04:52 +0100 |
---|---|---|
committer | Nick Thomas <nick@gitlab.com> | 2019-10-18 11:47:25 +0100 |
commit | 83d11f4deeb20b852a0af3433190a0f7250a0027 (patch) | |
tree | 1a9df18d6f9f59712c6f5c98e995a4918eb94a11 /go/internal/command/lfsauthenticate/lfsauthenticate.go | |
parent | 7d5229db263a62661653431881bef8b46984d0de (diff) | |
download | gitlab-shell-83d11f4deeb20b852a0af3433190a0f7250a0027.tar.gz |
Move go code up one level
Diffstat (limited to 'go/internal/command/lfsauthenticate/lfsauthenticate.go')
-rw-r--r-- | go/internal/command/lfsauthenticate/lfsauthenticate.go | 104 |
1 files changed, 0 insertions, 104 deletions
diff --git a/go/internal/command/lfsauthenticate/lfsauthenticate.go b/go/internal/command/lfsauthenticate/lfsauthenticate.go deleted file mode 100644 index bff5e7f..0000000 --- a/go/internal/command/lfsauthenticate/lfsauthenticate.go +++ /dev/null @@ -1,104 +0,0 @@ -package lfsauthenticate - -import ( - "encoding/base64" - "encoding/json" - "fmt" - - "gitlab.com/gitlab-org/gitlab-shell/go/internal/command/commandargs" - "gitlab.com/gitlab-org/gitlab-shell/go/internal/command/readwriter" - "gitlab.com/gitlab-org/gitlab-shell/go/internal/command/shared/accessverifier" - "gitlab.com/gitlab-org/gitlab-shell/go/internal/command/shared/disallowedcommand" - "gitlab.com/gitlab-org/gitlab-shell/go/internal/config" - "gitlab.com/gitlab-org/gitlab-shell/go/internal/gitlabnet/lfsauthenticate" -) - -const ( - downloadAction = "download" - uploadAction = "upload" -) - -type Command struct { - Config *config.Config - Args *commandargs.Shell - ReadWriter *readwriter.ReadWriter -} - -type PayloadHeader struct { - Auth string `json:"Authorization"` -} - -type Payload struct { - Header PayloadHeader `json:"header"` - Href string `json:"href"` - ExpiresIn int `json:"expires_in,omitempty"` -} - -func (c *Command) Execute() error { - args := c.Args.SshArgs - if len(args) < 3 { - return disallowedcommand.Error - } - - repo := args[1] - action, err := actionToCommandType(args[2]) - if err != nil { - return err - } - - accessResponse, err := c.verifyAccess(action, repo) - if err != nil { - return err - } - - payload, err := c.authenticate(action, repo, accessResponse.UserId) - if err != nil { - // return nothing just like Ruby's GitlabShell#lfs_authenticate does - return nil - } - - fmt.Fprintf(c.ReadWriter.Out, "%s\n", payload) - - return nil -} - -func actionToCommandType(action string) (commandargs.CommandType, error) { - var accessAction commandargs.CommandType - switch action { - case downloadAction: - accessAction = commandargs.UploadPack - case uploadAction: - accessAction = commandargs.ReceivePack - default: - return "", disallowedcommand.Error - } - - return accessAction, nil -} - -func (c *Command) verifyAccess(action commandargs.CommandType, repo string) (*accessverifier.Response, error) { - cmd := accessverifier.Command{c.Config, c.Args, c.ReadWriter} - - return cmd.Verify(action, repo) -} - -func (c *Command) authenticate(action commandargs.CommandType, repo, userId string) ([]byte, error) { - client, err := lfsauthenticate.NewClient(c.Config, c.Args) - if err != nil { - return nil, err - } - - response, err := client.Authenticate(action, repo, userId) - if err != nil { - return nil, err - } - - basicAuth := base64.StdEncoding.EncodeToString([]byte(response.Username + ":" + response.LfsToken)) - payload := &Payload{ - Header: PayloadHeader{Auth: "Basic " + basicAuth}, - Href: response.RepoPath + "/info/lfs", - ExpiresIn: response.ExpiresIn, - } - - return json.Marshal(payload) -} |