-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
implement first version of github auth
- Loading branch information
Showing
19 changed files
with
603 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
package examples | ||
|
||
import ( | ||
"context" | ||
"encoding/base64" | ||
"encoding/json" | ||
"fmt" | ||
"net/url" | ||
|
||
"github.com/mantil-io/mantil.go/logs" | ||
"github.com/mantil-io/mantil/cli/secret" | ||
"github.com/mantil-io/mantil/domain" | ||
"github.com/nats-io/nats.go" | ||
"github.com/pkg/browser" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
const ( | ||
clientID = "db4946aabe86cd6c126e" | ||
) | ||
|
||
func NewGithubAuthCommand() *cobra.Command { | ||
cmd := &cobra.Command{ | ||
Use: "gh-auth", | ||
Short: "authenticate to github", | ||
Hidden: true, | ||
RunE: func(cmd *cobra.Command, args []string) error { | ||
node := cmd.Flag("node").Value.String() | ||
n, err := findNode(node) | ||
if err != nil { | ||
return err | ||
} | ||
s, err := createState(n) | ||
if err != nil { | ||
return err | ||
} | ||
if err := githubLogin(s); err != nil { | ||
return err | ||
} | ||
if err := waitToken(s.Inbox); err != nil { | ||
return err | ||
} | ||
return nil | ||
}, | ||
} | ||
cmd.Flags().StringP("node", "", domain.DefaultNodeName, "") | ||
return cmd | ||
} | ||
|
||
func findNode(name string) (*domain.Node, error) { | ||
fs, err := domain.NewSingleDeveloperWorkspaceStore() | ||
if err != nil { | ||
return nil, err | ||
} | ||
w := fs.Workspace() | ||
if len(w.Nodes) == 0 { | ||
return nil, fmt.Errorf("no nodes avaiable") | ||
} | ||
n := w.FindNode(name) | ||
if n == nil { | ||
return nil, fmt.Errorf("node not found") | ||
} | ||
return n, nil | ||
} | ||
|
||
type state struct { | ||
Inbox string `json:"inbox"` | ||
NodeEndpoint string `json:"node_endpoint"` | ||
} | ||
|
||
func createState(n *domain.Node) (*state, error) { | ||
inbox := nats.NewInbox() | ||
s := state{ | ||
Inbox: inbox, | ||
NodeEndpoint: n.Endpoints.Rest, | ||
} | ||
return &s, nil | ||
} | ||
|
||
func githubLogin(state *state) error { | ||
u, err := url.Parse("https://github.com/login/oauth/authorize") | ||
if err != nil { | ||
return err | ||
} | ||
q := u.Query() | ||
q.Set("client_id", clientID) | ||
buf, err := json.Marshal(state) | ||
if err != nil { | ||
return err | ||
} | ||
sb64 := base64.StdEncoding.EncodeToString([]byte(buf)) | ||
q.Set("state", sb64) | ||
u.RawQuery = q.Encode() | ||
return browser.OpenURL(u.String()) | ||
} | ||
|
||
func waitToken(inbox string) error { | ||
rsp := struct { | ||
JWT string `json:"jwt"` | ||
}{} | ||
lc := logs.ListenerConfig{ | ||
ListenerJWT: secret.LogsListenerCreds, | ||
Subject: inbox, | ||
Rsp: &rsp, | ||
} | ||
l, err := logs.NewLambdaListener(lc) | ||
if err != nil { | ||
return err | ||
} | ||
if err := l.Done(context.Background()); err != nil { | ||
return err | ||
} | ||
r, _ := json.MarshalIndent(rsp, "", " ") | ||
fmt.Println(string(r)) | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
package examples | ||
|
||
import ( | ||
"github.com/mantil-io/mantil/cli/controller/invoke" | ||
"github.com/mantil-io/mantil/cli/log" | ||
"github.com/mantil-io/mantil/cli/ui" | ||
"github.com/mantil-io/mantil/domain" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
func NewUserCommand() *cobra.Command { | ||
cmd := &cobra.Command{ | ||
Use: "user", | ||
Hidden: true, | ||
} | ||
cmd.AddCommand(NewUserAddCommand()) | ||
return cmd | ||
} | ||
|
||
type AddUserRequest struct { | ||
Username string `json:"username"` | ||
} | ||
|
||
func NewUserAddCommand() *cobra.Command { | ||
cmd := &cobra.Command{ | ||
Use: "add", | ||
Hidden: true, | ||
Args: cobra.ExactArgs(1), | ||
RunE: func(cmd *cobra.Command, args []string) error { | ||
node := cmd.Flag("node").Value.String() | ||
n, err := findNode(node) | ||
if err != nil { | ||
return err | ||
} | ||
i, err := nodeInvoker(n) | ||
if err != nil { | ||
return err | ||
} | ||
return i.Do("auth/addUser", &AddUserRequest{ | ||
Username: args[0], | ||
}, nil) | ||
}, | ||
} | ||
cmd.Flags().StringP("node", "", domain.DefaultNodeName, "") | ||
return cmd | ||
} | ||
|
||
func NewProjectCommand() *cobra.Command { | ||
cmd := &cobra.Command{ | ||
Use: "project", | ||
Hidden: true, | ||
} | ||
cmd.AddCommand(NewProjectAddCommand()) | ||
return cmd | ||
} | ||
|
||
type AddProjectRequest struct { | ||
Repo string `json:"repo"` | ||
} | ||
|
||
func NewProjectAddCommand() *cobra.Command { | ||
cmd := &cobra.Command{ | ||
Use: "add", | ||
Hidden: true, | ||
Args: cobra.ExactArgs(1), | ||
RunE: func(cmd *cobra.Command, args []string) error { | ||
node := cmd.Flag("node").Value.String() | ||
n, err := findNode(node) | ||
if err != nil { | ||
return err | ||
} | ||
i, err := nodeInvoker(n) | ||
if err != nil { | ||
return err | ||
} | ||
return i.Do("auth/addProject", &AddProjectRequest{ | ||
Repo: args[0], | ||
}, nil) | ||
}, | ||
} | ||
cmd.Flags().StringP("node", "", domain.DefaultNodeName, "") | ||
return cmd | ||
} | ||
|
||
func nodeInvoker(node *domain.Node) (*invoke.HTTPClient, error) { | ||
token, err := node.AuthToken() | ||
if err != nil { | ||
return nil, log.Wrap(err) | ||
} | ||
return invoke.Node(node.Endpoints.Rest, token, ui.NodeLogsSink), nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.