Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Up Authorization #31

Merged
merged 1 commit into from
Oct 19, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion gateway/envs.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ func (g *Gateway) GetEnvs(ctx context.Context, req *entity.GetEnvsRequest) (*ent
gqlReq.Var("projectId", req.ProjectID)
gqlReq.Var("environmentId", req.EnvironmentID)

err := g.authorize(ctx, gqlReq)
err := g.authorize(ctx, gqlReq.Header)

if err != nil {
return nil, err
Expand Down
5 changes: 3 additions & 2 deletions gateway/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package gateway
import (
"context"
"fmt"
"net/http"

gql "github.com/machinebox/graphql"
configs "github.com/railwayapp/cli/configs"
Expand All @@ -14,12 +15,12 @@ type Gateway struct {
gqlClient *gql.Client
}

func (g *Gateway) authorize(ctx context.Context, req *gql.Request) error {
func (g *Gateway) authorize(ctx context.Context, header http.Header) error {
user, err := g.cfg.GetUserConfigs()
if err != nil {
return err
}
req.Header.Add("authorization", fmt.Sprintf("Bearer %s", user.Token))
header.Add("authorization", fmt.Sprintf("Bearer %s", user.Token))
return nil
}

Expand Down
10 changes: 5 additions & 5 deletions gateway/project.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ func (g *Gateway) GetProject(ctx context.Context, projectId string) (*entity.Pro

gqlReq.Var("projectId", projectId)

g.authorize(ctx, gqlReq)
g.authorize(ctx, gqlReq.Header)

var resp struct {
Project *entity.Project `json:"projectById"`
Expand All @@ -58,7 +58,7 @@ func (g *Gateway) CreateProject(ctx context.Context, req *entity.CreateProjectRe
}
`)

g.authorize(ctx, gqlReq)
g.authorize(ctx, gqlReq.Header)

gqlReq.Var("name", req.Name)

Expand All @@ -81,7 +81,7 @@ func (g *Gateway) UpdateProject(ctx context.Context, req *entity.UpdateProjectRe
}
`)

err := g.authorize(ctx, gqlReq)
err := g.authorize(ctx, gqlReq.Header)

if err != nil {
return nil, err
Expand All @@ -104,7 +104,7 @@ func (g *Gateway) DeleteProject(ctx context.Context, projectId string) error {
}
`)

err := g.authorize(ctx, gqlReq)
err := g.authorize(ctx, gqlReq.Header)

if err != nil {
return err
Expand Down Expand Up @@ -141,7 +141,7 @@ func (g *Gateway) GetProjects(ctx context.Context) ([]*entity.Project, error) {
`)

// TODO build this into the GQL client
err := g.authorize(ctx, gqlReq)
err := g.authorize(ctx, gqlReq.Header)

if err != nil {
return nil, err
Expand Down
1 change: 1 addition & 0 deletions gateway/up.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ func (g *Gateway) Up(ctx context.Context, req *entity.UpRequest) (*entity.UpResp
if err != nil {
return nil, err
}
g.authorize(ctx, httpReq.Header)
client := &http.Client{}
resp, err := client.Do(httpReq)
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion gateway/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ func (g *Gateway) GetUser(ctx context.Context) (*entity.User, error) {
}
`)

err := g.authorize(ctx, gqlReq)
err := g.authorize(ctx, gqlReq.Header)

if err != nil {
return nil, err
Expand Down