update app example
This commit is contained in:
parent
5a98ec4623
commit
3c2ad6a53d
1 changed files with 29 additions and 32 deletions
|
@ -6,10 +6,11 @@ import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"net/http"
|
"net/http"
|
||||||
"os"
|
"os"
|
||||||
|
"time"
|
||||||
"github.com/sirupsen/logrus"
|
|
||||||
|
|
||||||
"github.com/google/uuid"
|
"github.com/google/uuid"
|
||||||
|
"github.com/sirupsen/logrus"
|
||||||
|
"golang.org/x/oauth2"
|
||||||
|
|
||||||
"github.com/caos/oidc/pkg/oidc"
|
"github.com/caos/oidc/pkg/oidc"
|
||||||
"github.com/caos/oidc/pkg/rp"
|
"github.com/caos/oidc/pkg/rp"
|
||||||
|
@ -29,41 +30,34 @@ func main() {
|
||||||
|
|
||||||
ctx := context.Background()
|
ctx := context.Background()
|
||||||
|
|
||||||
rpConfig := &rp.Config{
|
rpConfig := &rp.Configuration{
|
||||||
|
Issuer: issuer,
|
||||||
|
Config: &oauth2.Config{
|
||||||
ClientID: clientID,
|
ClientID: clientID,
|
||||||
ClientSecret: clientSecret,
|
ClientSecret: clientSecret,
|
||||||
Issuer: issuer,
|
RedirectURL: fmt.Sprintf("http://localhost:%v%v", port, callbackPath),
|
||||||
CallbackURL: fmt.Sprintf("http://localhost:%v%v", port, callbackPath),
|
Scopes: []string{oidc.ScopeOpenID, oidc.ScopeProfile, oidc.ScopeEmail},
|
||||||
Scopes: []string{"openid", "profile", "email"},
|
},
|
||||||
}
|
}
|
||||||
cookieHandler := utils.NewCookieHandler(key, key, utils.WithUnsecure())
|
cookieHandler := utils.NewCookieHandler(key, key, utils.WithUnsecure())
|
||||||
provider, err := rp.NewDefaultRP(rpConfig, rp.WithCookieHandler(cookieHandler)) //rp.WithPKCE(cookieHandler)) //,
|
provider, err := rp.NewRelayingParty(rpConfig, rp.WithCookieHandler(cookieHandler), rp.WithPKCE(cookieHandler), rp.WithVerifierOpts(rp.WithIssuedAtOffset(-3*time.Minute))) //,
|
||||||
if err != nil {
|
if err != nil {
|
||||||
logrus.Fatalf("error creating provider %s", err.Error())
|
logrus.Fatalf("error creating provider %s", err.Error())
|
||||||
}
|
}
|
||||||
|
|
||||||
// state := "foobar"
|
//generate some state (representing the state of the user in your application,
|
||||||
state := uuid.New().String()
|
//e.g. the page where he was before sending him to login
|
||||||
|
state := func() string {
|
||||||
|
return uuid.New().String()
|
||||||
|
}
|
||||||
|
|
||||||
http.Handle("/login", provider.AuthURLHandler(state))
|
//register the AuthURLHandler at your preferred path
|
||||||
// http.HandleFunc("/login", func(w http.ResponseWriter, r *http.Request) {
|
//the AuthURLHandler creates the auth request and redirects the user to the auth server
|
||||||
// http.Redirect(w, r, provider.AuthURL(state), http.StatusFound)
|
//including state handling with secure cookie and the possibility to use PKCE
|
||||||
// })
|
http.Handle("/login", rp.AuthURLHandler(state, provider))
|
||||||
|
|
||||||
// http.HandleFunc(callbackPath, func(w http.ResponseWriter, r *http.Request) {
|
|
||||||
// tokens, err := provider.CodeExchange(ctx, r.URL.Query().Get("code"))
|
|
||||||
// if err != nil {
|
|
||||||
// http.Error(w, "failed to exchange token: "+err.Error(), http.StatusUnauthorized)
|
|
||||||
// return
|
|
||||||
// }
|
|
||||||
// data, err := json.Marshal(tokens)
|
|
||||||
// if err != nil {
|
|
||||||
// http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
||||||
// return
|
|
||||||
// }
|
|
||||||
// w.Write(data)
|
|
||||||
// })
|
|
||||||
|
|
||||||
|
//for demonstration purposes the returned tokens (access token, id_token an its parsed claims)
|
||||||
|
//are written as JSON objects onto response
|
||||||
marshal := func(w http.ResponseWriter, r *http.Request, tokens *oidc.Tokens, state string) {
|
marshal := func(w http.ResponseWriter, r *http.Request, tokens *oidc.Tokens, state string) {
|
||||||
_ = state
|
_ = state
|
||||||
data, err := json.Marshal(tokens)
|
data, err := json.Marshal(tokens)
|
||||||
|
@ -74,10 +68,13 @@ func main() {
|
||||||
w.Write(data)
|
w.Write(data)
|
||||||
}
|
}
|
||||||
|
|
||||||
http.Handle(callbackPath, provider.CodeExchangeHandler(marshal))
|
//register the CodeExchangeHandler at the callbackPath
|
||||||
|
//the CodeExchangeHandler handles the auth response, creates the token request and calls the callback function
|
||||||
|
//with the returned tokens from the token endpoint
|
||||||
|
http.Handle(callbackPath, rp.CodeExchangeHandler(marshal, provider))
|
||||||
|
|
||||||
http.HandleFunc("/test", func(w http.ResponseWriter, r *http.Request) {
|
http.HandleFunc("/test", func(w http.ResponseWriter, r *http.Request) {
|
||||||
tokens, err := provider.ClientCredentials(ctx, "scope")
|
tokens, err := rp.ClientCredentials(ctx, provider, "scope")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
http.Error(w, "failed to exchange token: "+err.Error(), http.StatusUnauthorized)
|
http.Error(w, "failed to exchange token: "+err.Error(), http.StatusUnauthorized)
|
||||||
return
|
return
|
||||||
|
@ -92,5 +89,5 @@ func main() {
|
||||||
})
|
})
|
||||||
lis := fmt.Sprintf("127.0.0.1:%s", port)
|
lis := fmt.Sprintf("127.0.0.1:%s", port)
|
||||||
logrus.Infof("listening on http://%s/", lis)
|
logrus.Infof("listening on http://%s/", lis)
|
||||||
logrus.Fatal(http.ListenAndServe("127.0.0.1:5556", nil))
|
logrus.Fatal(http.ListenAndServe("127.0.0.1:"+port, nil))
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue