-
Notifications
You must be signed in to change notification settings - Fork 128
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add authentication using OAuth2 and PCKE
- Loading branch information
Showing
7 changed files
with
194 additions
and
34 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,82 @@ | ||
package authentication | ||
|
||
import ( | ||
"context" | ||
"crypto/tls" | ||
"fmt" | ||
"github.com/skratchdot/open-golang/open" | ||
"golang.org/x/oauth2" | ||
"io" | ||
"log" | ||
"net/http" | ||
"net/url" | ||
"time" | ||
) | ||
|
||
var ( | ||
conf *oauth2.Config | ||
ctx context.Context | ||
verifier string | ||
authToken string | ||
) | ||
|
||
func callbackHandler(w http.ResponseWriter, r *http.Request) { | ||
queryParts, _ := url.ParseQuery(r.URL.RawQuery) | ||
|
||
code := queryParts["code"][0] | ||
|
||
tok, err := conf.Exchange(ctx, code, oauth2.VerifierOption(verifier)) | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
authToken = tok.AccessToken | ||
io.WriteString(w, "Login successful! Please close this window and return back to CLI") | ||
} | ||
|
||
func serve(done chan int) { | ||
log.Fatal(http.ListenAndServe(":9998", nil)) | ||
if done != nil { | ||
done <- 0 | ||
} | ||
} | ||
|
||
func VerifyLogin(clientID string, clientSecret string) (string, error) { | ||
authToken = "" | ||
ctx = context.Background() | ||
conf = &oauth2.Config{ | ||
ClientID: clientID, | ||
ClientSecret: clientSecret, | ||
Scopes: []string{"openid"}, | ||
Endpoint: oauth2.Endpoint{ | ||
AuthURL: "https://sso.stage.redhat.com/auth/realms/redhat-external/protocol/openid-connect/auth", | ||
TokenURL: "https://sso.stage.redhat.com/auth/realms/redhat-external/protocol/openid-connect/token", | ||
}, | ||
RedirectURL: "http://127.0.0.1:9998/oauth/callback", | ||
} | ||
verifier = oauth2.GenerateVerifier() | ||
|
||
tr := &http.Transport{ | ||
TLSClientConfig: &tls.Config{InsecureSkipVerify: true}, | ||
} | ||
sslcli := &http.Client{Transport: tr} | ||
ctx = context.WithValue(ctx, oauth2.HTTPClient, sslcli) | ||
url := conf.AuthCodeURL("state", oauth2.AccessTypeOffline, oauth2.S256ChallengeOption(verifier)) | ||
|
||
http.HandleFunc("/oauth/callback", callbackHandler) | ||
done := make(chan int) | ||
twoMinTimer := time.Now().Local().Add(time.Minute * 2) | ||
go serve(done) | ||
time.Sleep(2 * time.Second) | ||
open.Run(url) | ||
time.Sleep(1 * time.Second) | ||
for { | ||
if authToken != "" { | ||
return authToken, nil | ||
} | ||
if time.Now().After(twoMinTimer) { | ||
return authToken, fmt.Errorf("Time expired") | ||
} | ||
} | ||
<-done | ||
return authToken, fmt.Errorf("An error occurred while verifying authentication") | ||
} |
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,54 @@ | ||
// This example shows how to retrieve the collection of capabilities. | ||
|
||
package main | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"github.com/openshift-online/ocm-sdk-go/authentication" | ||
"os" | ||
|
||
sdk "github.com/openshift-online/ocm-sdk-go" | ||
"github.com/openshift-online/ocm-sdk-go/logging" | ||
) | ||
|
||
func main() { | ||
// Create a context: | ||
ctx := context.Background() | ||
|
||
// Create a logger that has the debug level enabled: | ||
logger, err := logging.NewGoLoggerBuilder(). | ||
Debug(true). | ||
Build() | ||
if err != nil { | ||
fmt.Fprintf(os.Stderr, "Can't build logger: %v\n", err) | ||
os.Exit(1) | ||
} | ||
|
||
// Create the connection, and remember to close it: | ||
token, err := authentication.VerifyLogin("cloud-services", "") | ||
if err != nil { | ||
fmt.Fprintf(os.Stderr, "Can't get token: %v\n", err) | ||
os.Exit(1) | ||
} | ||
connection, err := sdk.NewConnectionBuilder(). | ||
Logger(logger). | ||
Tokens(token). | ||
URL("http://localhost:8000"). | ||
BuildContext(ctx) | ||
if err != nil { | ||
fmt.Fprintf(os.Stderr, "Can't build connection: %v\n", err) | ||
os.Exit(1) | ||
} | ||
defer connection.Close() | ||
|
||
collection := connection.AccountsMgmt().V1().CurrentAccount() | ||
|
||
// Retrieve current account information | ||
_, err = collection.Get(). | ||
SendContext(ctx) | ||
if err != nil { | ||
fmt.Fprintf(os.Stderr, "Can't retrieve capabilities: %s\n", err) | ||
os.Exit(1) | ||
} | ||
} |
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.