Skip to content

Instantly share code, notes, and snippets.

@wesleywillians
Created August 25, 2020 15:53
Show Gist options
  • Save wesleywillians/681ebbb1278e2f568065bcb28073e600 to your computer and use it in GitHub Desktop.
Save wesleywillians/681ebbb1278e2f568065bcb28073e600 to your computer and use it in GitHub Desktop.
Exemplo OpenID Connect /Keycloak
package main
import (
"context"
"encoding/json"
oidc "github.com/coreos/go-oidc"
"golang.org/x/oauth2"
"log"
"net/http"
)
var (
clientID = "app"
clientSecret = "1c5f824b-4408-45cd-947a-63549452aadd"
)
func main() {
ctx := context.Background()
provider, err := oidc.NewProvider(ctx, "http://localhost:8080/auth/realms/demo")
if err != nil {
log.Fatal(err)
}
config := oauth2.Config{
ClientID: clientID,
ClientSecret: clientSecret,
Endpoint: provider.Endpoint(),
RedirectURL: "http://localhost:8081/auth/callback",
Scopes: []string{oidc.ScopeOpenID, "profile", "email", "roles"},
}
state := "magica"
http.HandleFunc("/", func(writer http.ResponseWriter, request *http.Request) {
http.Redirect(writer, request, config.AuthCodeURL(state), http.StatusFound)
})
http.HandleFunc("/auth/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 excahge token", http.StatusBadRequest)
return
}
rawIDToken, ok := oauth2Token.Extra("id_token").(string)
if !ok {
http.Error(w, "no id_token", http.StatusBadRequest)
return
}
resp := struct {
OAuth2Token *oauth2.Token
RawIDToken string
}{
oauth2Token, rawIDToken,
}
data, err := json.MarshalIndent(resp, ""," ")
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
w.Write(data)
})
log.Fatal(http.ListenAndServe(":8081",nil))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment