-
Notifications
You must be signed in to change notification settings - Fork 147
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(cli): added implementation for codeflow with a cli (#26)
- Loading branch information
Showing
7 changed files
with
219 additions
and
18 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package main | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"github.com/caos/oidc/pkg/cli" | ||
"github.com/caos/oidc/pkg/rp" | ||
"github.com/google/go-github/v31/github" | ||
githubOAuth "golang.org/x/oauth2/github" | ||
"os" | ||
) | ||
|
||
var ( | ||
callbackPath string = "/orbctl/github/callback" | ||
key []byte = []byte("test1234test1234") | ||
) | ||
|
||
func main() { | ||
clientID := os.Getenv("CLIENT_ID") | ||
clientSecret := os.Getenv("CLIENT_SECRET") | ||
port := os.Getenv("PORT") | ||
|
||
rpConfig := &rp.Config{ | ||
ClientID: clientID, | ||
ClientSecret: clientSecret, | ||
CallbackURL: fmt.Sprintf("http://localhost:%v%v", port, callbackPath), | ||
Scopes: []string{"repo", "repo_deployment"}, | ||
Endpoints: githubOAuth.Endpoint, | ||
} | ||
|
||
oauth2Client := cli.CodeFlowForClient(rpConfig, key, callbackPath, port) | ||
|
||
client := github.NewClient(oauth2Client) | ||
|
||
ctx := context.Background() | ||
_, _, err := client.Users.Get(ctx, "") | ||
if err != nil { | ||
fmt.Println("OAuth flow failed") | ||
} else { | ||
|
||
fmt.Println("OAuth flow success") | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
package cli | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"github.com/caos/oidc/pkg/oidc" | ||
"github.com/caos/oidc/pkg/rp" | ||
"github.com/caos/oidc/pkg/utils" | ||
"github.com/google/uuid" | ||
"github.com/sirupsen/logrus" | ||
"log" | ||
"net/http" | ||
"strings" | ||
"time" | ||
) | ||
|
||
func CodeFlow(rpc *rp.Config, key []byte, callbackPath string, port string) *oidc.Tokens { | ||
cookieHandler := utils.NewCookieHandler(key, key, utils.WithUnsecure()) | ||
provider, err := rp.NewDefaultRP(rpc, rp.WithCookieHandler(cookieHandler)) //rp.WithPKCE(cookieHandler)) //, | ||
if err != nil { | ||
logrus.Fatalf("error creating provider %s", err.Error()) | ||
} | ||
|
||
return codeFlow(provider, callbackPath, port) | ||
} | ||
|
||
func CodeFlowForClient(rpc *rp.Config, key []byte, callbackPath string, port string) *http.Client { | ||
cookieHandler := utils.NewCookieHandler(key, key, utils.WithUnsecure()) | ||
provider, err := rp.NewDefaultRP(rpc, rp.WithCookieHandler(cookieHandler)) //rp.WithPKCE(cookieHandler)) //, | ||
if err != nil { | ||
logrus.Fatalf("error creating provider %s", err.Error()) | ||
} | ||
token := codeFlow(provider, callbackPath, port) | ||
|
||
return provider.Client(context.Background(), token.Token) | ||
} | ||
|
||
func codeFlow(provider rp.DelegationTokenExchangeRP, callbackPath string, port string) *oidc.Tokens { | ||
loginPath := "/login" | ||
portStr := port | ||
if !strings.HasPrefix(port, ":") { | ||
portStr = strings.Join([]string{":", portStr}, "") | ||
} | ||
|
||
getToken, setToken := getAndSetTokens() | ||
|
||
state := uuid.New().String() | ||
http.Handle(loginPath, provider.AuthURLHandler(state)) | ||
|
||
marshal := func(w http.ResponseWriter, r *http.Request, tokens *oidc.Tokens, state string) { | ||
setToken(w, tokens) | ||
} | ||
http.Handle(callbackPath, provider.CodeExchangeHandler(marshal)) | ||
|
||
// start http-server | ||
stopHttpServer := startHttpServer(portStr) | ||
|
||
// open browser in different window | ||
utils.OpenBrowser(strings.Join([]string{"http://localhost", portStr, loginPath}, "")) | ||
|
||
// wait until user is logged into browser | ||
ret := getToken() | ||
|
||
// stop http-server as no callback is needed anymore | ||
stopHttpServer() | ||
|
||
// return tokens | ||
return ret | ||
} | ||
|
||
func startHttpServer(port string) func() { | ||
srv := &http.Server{Addr: port} | ||
go func() { | ||
|
||
// always returns error. ErrServerClosed on graceful close | ||
if err := srv.ListenAndServe(); err != http.ErrServerClosed { | ||
// unexpected error. port in use? | ||
log.Fatalf("ListenAndServe(): %v", err) | ||
} | ||
}() | ||
|
||
return func() { | ||
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) | ||
defer cancel() | ||
|
||
if err := srv.Shutdown(ctx); err != nil { | ||
log.Fatalf("Shutdown(): %v", err) | ||
} | ||
} | ||
} | ||
|
||
func getAndSetTokens() (func() *oidc.Tokens, func(w http.ResponseWriter, tokens *oidc.Tokens)) { | ||
marshalChan := make(chan *oidc.Tokens) | ||
|
||
getToken := func() *oidc.Tokens { | ||
return <-marshalChan | ||
} | ||
setToken := func(w http.ResponseWriter, tokens *oidc.Tokens) { | ||
marshalChan <- tokens | ||
|
||
msg := "<p><strong>Success!</strong></p>" | ||
msg = msg + "<p>You are authenticated and can now return to the CLI.</p>" | ||
fmt.Fprintf(w, msg) | ||
} | ||
|
||
return getToken, setToken | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package utils | ||
|
||
import ( | ||
"fmt" | ||
"log" | ||
"os/exec" | ||
"runtime" | ||
) | ||
|
||
func OpenBrowser(url string) { | ||
var err error | ||
|
||
switch runtime.GOOS { | ||
case "linux": | ||
err = exec.Command("xdg-open", url).Start() | ||
case "windows": | ||
err = exec.Command("rundll32", "url.dll,FileProtocolHandler", url).Start() | ||
case "darwin": | ||
err = exec.Command("open", url).Start() | ||
default: | ||
err = fmt.Errorf("unsupported platform") | ||
} | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
} |