-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(cmd/get_jwt_from_oidc): Add debug tool for OIDC
- Loading branch information
1 parent
f3bcfa2
commit a601742
Showing
4 changed files
with
164 additions
and
0 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,105 @@ | ||
package get_jwt_from_oidc | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"log" | ||
"net/http" | ||
|
||
"github.com/coreos/go-oidc" | ||
"github.com/sikalabs/slr/cmd/root" | ||
"github.com/spf13/cobra" | ||
"golang.org/x/oauth2" | ||
) | ||
|
||
var FlagIssuer string | ||
var FlagClientID string | ||
var FlagClientSecret string | ||
|
||
var Cmd = &cobra.Command{ | ||
Use: "get-jwt-from-oidc", | ||
Short: "Get JWT from OIDC (Keycloak, Okta, ...)", | ||
Run: func(cmd *cobra.Command, args []string) { | ||
Server(FlagIssuer, FlagClientID, FlagClientSecret) | ||
}, | ||
} | ||
|
||
func init() { | ||
root.Cmd.AddCommand(Cmd) | ||
Cmd.Flags().StringVar( | ||
&FlagIssuer, | ||
"issuer", | ||
"", | ||
"Issuer", | ||
) | ||
Cmd.MarkFlagRequired("issuer") | ||
Cmd.Flags().StringVar( | ||
&FlagClientID, | ||
"client-id", | ||
"", | ||
"Client ID", | ||
) | ||
Cmd.MarkFlagRequired("client-id") | ||
Cmd.Flags().StringVar( | ||
&FlagClientSecret, | ||
"client-secret", | ||
"", | ||
"Client Secret", | ||
) | ||
Cmd.MarkFlagRequired("client-secret") | ||
} | ||
|
||
func Server(issuer, clientID, clientSecret string) { | ||
ctx := context.Background() | ||
|
||
provider, err := oidc.NewProvider(ctx, issuer) | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
|
||
config := oauth2.Config{ | ||
ClientID: clientID, | ||
ClientSecret: clientSecret, | ||
Endpoint: provider.Endpoint(), | ||
RedirectURL: "http://localhost:8000/callback", | ||
Scopes: []string{oidc.ScopeOpenID, "profile", "email"}, | ||
} | ||
|
||
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { | ||
http.Redirect(w, r, config.AuthCodeURL("state"), http.StatusFound) | ||
}) | ||
|
||
http.HandleFunc("/callback", func(w http.ResponseWriter, r *http.Request) { | ||
if r.URL.Query().Get("state") != "state" { | ||
http.Error(w, "state did not match", http.StatusBadRequest) | ||
return | ||
} | ||
|
||
oauth2Token, err := config.Exchange(ctx, r.URL.Query().Get("code")) | ||
if err != nil { | ||
http.Error(w, "Failed to exchange token: "+err.Error(), http.StatusInternalServerError) | ||
return | ||
} | ||
|
||
rawIDToken, ok := oauth2Token.Extra("id_token").(string) | ||
if !ok { | ||
http.Error(w, "No id_token field in oauth2 token.", http.StatusInternalServerError) | ||
return | ||
} | ||
|
||
idToken, err := provider.Verifier(&oidc.Config{ClientID: clientID}).Verify(ctx, rawIDToken) | ||
if err != nil { | ||
http.Error(w, "Failed to verify ID Token: "+err.Error(), http.StatusInternalServerError) | ||
return | ||
} | ||
|
||
_ = idToken // ID Token is now verified and can be used | ||
|
||
fmt.Println(rawIDToken) | ||
|
||
fmt.Fprintf(w, "%s\n", rawIDToken) | ||
}) | ||
|
||
fmt.Println("http://localhost:8000/") | ||
log.Fatal(http.ListenAndServe(":8000", 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