-
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.
- Loading branch information
Showing
21 changed files
with
341 additions
and
71 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,101 @@ | ||
package controller | ||
|
||
import ( | ||
"context" | ||
"encoding/base64" | ||
"encoding/json" | ||
"errors" | ||
"net/url" | ||
|
||
"github.com/mantil-io/mantil.go/logs" | ||
"github.com/mantil-io/mantil/cli/log" | ||
"github.com/mantil-io/mantil/cli/secret" | ||
"github.com/mantil-io/mantil/domain" | ||
"github.com/nats-io/nats.go" | ||
"github.com/pkg/browser" | ||
) | ||
|
||
const ( | ||
clientID = "db4946aabe86cd6c126e" | ||
) | ||
|
||
func authToken(n *domain.Node) (string, error) { | ||
t, err := n.AuthToken() | ||
var terr *domain.TokenExpiredError | ||
if errors.As(err, &terr) && n.GitHubAuthEnabled { | ||
var err error | ||
t, err = githubAuth(n) | ||
if err != nil { | ||
return "", log.Wrap(err) | ||
} | ||
n.JWT = t | ||
} else if err != nil { | ||
return "", log.Wrap(err) | ||
} | ||
return t, nil | ||
} | ||
|
||
func githubAuth(n *domain.Node) (string, error) { | ||
s, err := createState(n) | ||
if err != nil { | ||
return "", err | ||
} | ||
if err := githubLogin(s); err != nil { | ||
return "", err | ||
} | ||
t, err := waitToken(s.Inbox) | ||
if err != nil { | ||
return "", err | ||
} | ||
return t, 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) (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 | ||
} | ||
return rsp.JWT, 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
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
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.