#601·goth

no provider for openid-connect exists

Author: habsfanongitCreated Apr 1, 2025Updated May 22, 2025

I keep getting no provider for openid-connect exists when I try to connect to local keycloak.

package main

import (
	"fmt"
	"html/template"
	"net/http"

	"github.com/markbates/goth"
	"github.com/markbates/goth/gothic"
	"github.com/markbates/goth/providers/openidConnect"
)

func main() {

	openidConnect, _ := openidConnect.New("idp", "SECRET", "http://localhost:7080/realms/svcs/.well-known/openid-configuration", "http://localhost:8080/auth/openid-connect/callback")

	if openidConnect != nil {
		goth.UseProviders(openidConnect)
	}
	// Note that the redirect URL is different here than in your original code.
	//goth.UseProviders(oid)

	r := http.NewServeMux()
	r.HandleFunc("GET /auth/openid-connect/callback", func(res http.ResponseWriter, r *http.Request) {
		q := r.URL.Query()
		q.Add("provider", "openid-connect")
		r.URL.RawQuery = q.Encode()
		user, err := gothic.CompleteUserAuth(res, r)
		if err != nil {
			fmt.Println("error ", err)
		}

		t, _ := template.New("foo").Parse(userTemplate)
		t.Execute(res, user)
	})
	r.HandleFunc("GET /auth/openid-connect", handleAuth)

	r.HandleFunc("/", func(writer http.ResponseWriter, r *http.Request) {

		t, _ := template.New("foo").Parse(indexTemplate)
		t.Execute(writer, nil)
	})

	http.ListenAndServe(":8080", r)

}
func handleAuth(w http.ResponseWriter, r *http.Request) {
	q := r.URL.Query()
	q.Add("provider", "openid-connect")
	r.URL.RawQuery = q.Encode()

	gothic.BeginAuthHandler(w, r)
}

var indexTemplate = `
<p><a href="/auth/openid-connect">Log in</a></p>
`

var userTemplate = `
<p>Name: {{.Name}}</p>
<p>Email: {{.Email}}</p>
<p>NickName: {{.NickName}}</p>
<p>Location: {{.Location}}</p>
<p>AvatarURL: {{.AvatarURL}} <img src="{{.AvatarURL}}"></p>
<p>Description: {{.Description}}</p>
<p>UserID: {{.UserID}}</p>
<p>AccessToken: {{.AccessToken}}</p>
`

Keycloak is running and the client is set. What am missing?