-
-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(cmd/example_server_oidc): Create example server with OIDC auth
- Loading branch information
1 parent
df01ccc
commit aa39de8
Showing
4 changed files
with
100 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,93 @@ | ||
package example_server_oidc | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"log" | ||
"net/http" | ||
|
||
"github.com/coreos/go-oidc" | ||
"github.com/sikalabs/slu/cmd/root" | ||
"github.com/spf13/cobra" | ||
"golang.org/x/oauth2" | ||
) | ||
|
||
var FlagPort int | ||
var FlagIssuer string | ||
var FlagClientID string | ||
var FlagClientSecret string | ||
|
||
func init() { | ||
root.RootCmd.AddCommand(Cmd) | ||
Cmd.PersistentFlags().IntVarP(&FlagPort, "port", "p", 8000, "Listen on port") | ||
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") | ||
} | ||
|
||
var Cmd = &cobra.Command{ | ||
Use: "example-server-oidc", | ||
Short: "Run example web server with OIDC", | ||
Args: cobra.NoArgs, | ||
Run: func(c *cobra.Command, args []string) { | ||
Server(FlagPort, FlagIssuer, FlagClientID, FlagClientSecret) | ||
}, | ||
} | ||
|
||
func Server(port int, 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: fmt.Sprintf("http://127.0.0.1:%d/callback", port), | ||
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, "Login successful! %s", rawIDToken) | ||
}) | ||
|
||
fmt.Printf("http://127.0.0.1:%d\n", port) | ||
log.Fatal(http.ListenAndServe(fmt.Sprintf(":%d", port), 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