| 1 |
package main |
| 2 |
|
| 3 |
import ( |
| 4 |
"context" |
| 5 |
"crypto/sha256" |
| 6 |
"fmt" |
| 7 |
"log" |
| 8 |
"net/http" |
| 9 |
|
| 10 |
"github.com/go-chi/chi/v5" |
| 11 |
"golang.org/x/text/language" |
| 12 |
|
| 13 |
"github.com/zitadel/oidc/v3/example/server/storage" |
| 14 |
"github.com/zitadel/oidc/v3/pkg/op" |
| 15 |
) |
| 16 |
|
| 17 |
const ( |
| 18 |
pathLoggedOut = "/logged-out" |
| 19 |
) |
| 20 |
|
| 21 |
var ( |
| 22 |
hostnames = []string{ |
| 23 |
"localhost", //note that calling 127.0.0.1 / ::1 won't work as the hostname does not match |
| 24 |
"oidc.local", //add this to your hosts file (pointing to 127.0.0.1) |
| 25 |
//feel free to add more... |
| 26 |
} |
| 27 |
) |
| 28 |
|
| 29 |
func init() { |
| 30 |
storage.RegisterClients( |
| 31 |
storage.NativeClient("native"), |
| 32 |
storage.WebClient("web", "secret"), |
| 33 |
storage.WebClient("api", "secret"), |
| 34 |
) |
| 35 |
} |
| 36 |
|
| 37 |
func main() { |
| 38 |
ctx := context.Background() |
| 39 |
|
| 40 |
port := "9998" |
| 41 |
issuers := make([]string, len(hostnames)) |
| 42 |
for i, hostname := range hostnames { |
| 43 |
issuers[i] = fmt.Sprintf("http://%s:%s/", hostname, port) |
| 44 |
} |
| 45 |
|
| 46 |
//the OpenID Provider requires a 32-byte key for (token) encryption |
| 47 |
//be sure to create a proper crypto random key and manage it securely! |
| 48 |
key := sha256.Sum256([]byte("test")) |
| 49 |
keyId := "key1" |
| 50 |
|
| 51 |
router := chi.NewRouter() |
| 52 |
|
| 53 |
//for simplicity, we provide a very small default page for users who have signed out |
| 54 |
router.HandleFunc(pathLoggedOut, func(w http.ResponseWriter, req *http.Request) { |
| 55 |
_, err := w.Write([]byte("signed out successfully")) |
| 56 |
if err != nil { |
| 57 |
log.Printf("error serving logged out page: %v", err) |
| 58 |
} |
| 59 |
}) |
| 60 |
|
| 61 |
//the OpenIDProvider interface needs a Storage interface handling various checks and state manipulations |
| 62 |
//this might be the layer for accessing your database |
| 63 |
//in this example it will be handled in-memory |
| 64 |
//the NewMultiStorage is able to handle multiple issuers |
| 65 |
storage := storage.NewMultiStorage(issuers) |
| 66 |
|
| 67 |
//creation of the OpenIDProvider with the just created in-memory Storage |
| 68 |
provider, err := newDynamicOP(ctx, storage, key, keyId) |
| 69 |
if err != nil { |
| 70 |
log.Fatal(err) |
| 71 |
} |
| 72 |
|
| 73 |
//the provider will only take care of the OpenID Protocol, so there must be some sort of UI for the login process |
| 74 |
//for the simplicity of the example this means a simple page with username and password field |
| 75 |
//be sure to provide an IssuerInterceptor with the IssuerFromRequest from the OP so the login can select / and pass it to the storage |
| 76 |
l := NewLogin(storage, op.AuthCallbackURL(provider), op.NewIssuerInterceptor(provider.IssuerFromRequest)) |
| 77 |
|
| 78 |
//regardless of how many pages / steps there are in the process, the UI must be registered in the router, |
| 79 |
//so we will direct all calls to /login to the login UI |
| 80 |
router.Mount("/login/", http.StripPrefix("/login", l.router)) |
| 81 |
|
| 82 |
//we register the http handler of the OP on the root, so that the discovery endpoint (/.well-known/openid-configuration) |
| 83 |
//is served on the correct path |
| 84 |
// |
| 85 |
//if your issuer ends with a path (e.g. http://localhost:9998/custom/path/), |
| 86 |
//then you would have to set the path prefix (/custom/path/): |
| 87 |
//router.PathPrefix("/custom/path/").Handler(http.StripPrefix("/custom/path", provider.HttpHandler())) |
| 88 |
router.Mount("/", provider) |
| 89 |
|
| 90 |
server := &http.Server{ |
| 91 |
Addr: ":" + port, |
| 92 |
Handler: router, |
| 93 |
} |
| 94 |
err = server.ListenAndServe() |
| 95 |
if err != nil { |
| 96 |
log.Fatal(err) |
| 97 |
} |
| 98 |
<-ctx.Done() |
| 99 |
} |
| 100 |
|
| 101 |
// newDynamicOP will create an OpenID Provider for localhost on a specified port with a given encryption key |
| 102 |
// and a predefined default logout uri |
| 103 |
// it will enable all options (see descriptions) |
| 104 |
func newDynamicOP(ctx context.Context, storage op.Storage, key [32]byte, keyId string) (*op.Provider, error) { |
| 105 |
config := &op.Config{ |
| 106 |
CryptoKey: key, |
| 107 |
CryptoKeyId: keyId, |
| 108 |
|
| 109 |
//will be used if the end_session endpoint is called without a post_logout_redirect_uri |
| 110 |
DefaultLogoutRedirectURI: pathLoggedOut, |
| 111 |
|
| 112 |
//enables code_challenge_method S256 for PKCE (and therefore PKCE in general) |
| 113 |
CodeMethodS256: true, |
| 114 |
|
| 115 |
//enables additional client_id/client_secret authentication by form post (not only HTTP Basic Auth) |
| 116 |
AuthMethodPost: true, |
| 117 |
|
| 118 |
//enables additional authentication by using private_key_jwt |
| 119 |
AuthMethodPrivateKeyJWT: true, |
| 120 |
|
| 121 |
//enables refresh_token grant use |
| 122 |
GrantTypeRefreshToken: true, |
| 123 |
|
| 124 |
//enables use of the `request` Object parameter |
| 125 |
RequestObjectSupported: true, |
| 126 |
|
| 127 |
//this example has only static texts (in English), so we'll set the here accordingly |
| 128 |
SupportedUILocales: []language.Tag{language.English}, |
| 129 |
} |
| 130 |
handler, err := op.NewDynamicOpenIDProvider("/", config, storage, |
| 131 |
//we must explicitly allow the use of the http issuer |
| 132 |
op.WithAllowInsecure(), |
| 133 |
//as an example on how to customize an endpoint this will change the authorization_endpoint from /authorize to /auth |
| 134 |
op.WithCustomAuthEndpoint(op.NewEndpoint("auth")), |
| 135 |
) |
| 136 |
if err != nil { |
| 137 |
return nil, err |
| 138 |
} |
| 139 |
return handler, nil |
| 140 |
} |