From 36a5346885defbdba9d9fd5f33a3806f7bdd0a70 Mon Sep 17 00:00:00 2001
From: chapeau
Date: Wed, 7 Feb 2024 16:45:25 +0100
Subject: [PATCH] Example app
---
.gitignore | 1 +
LICENSE | 202 ++++++++++++++++++++++++++++++
README.md | 9 ++
go.mod | 19 +++
go.sum | 68 +++++++++++
main.go | 339 +++++++++++++++++++++++++++++++++++++++++++++++++++
templates.go | 110 +++++++++++++++++
7 files changed, 748 insertions(+)
create mode 100644 .gitignore
create mode 100644 LICENSE
create mode 100644 README.md
create mode 100644 go.mod
create mode 100644 go.sum
create mode 100644 main.go
create mode 100644 templates.go
diff --git a/.gitignore b/.gitignore
new file mode 100644
index 0000000..6f30a3a
--- /dev/null
+++ b/.gitignore
@@ -0,0 +1 @@
+/example
diff --git a/LICENSE b/LICENSE
new file mode 100644
index 0000000..d645695
--- /dev/null
+++ b/LICENSE
@@ -0,0 +1,202 @@
+
+ Apache License
+ Version 2.0, January 2004
+ http://www.apache.org/licenses/
+
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
+
+ 1. Definitions.
+
+ "License" shall mean the terms and conditions for use, reproduction,
+ and distribution as defined by Sections 1 through 9 of this document.
+
+ "Licensor" shall mean the copyright owner or entity authorized by
+ the copyright owner that is granting the License.
+
+ "Legal Entity" shall mean the union of the acting entity and all
+ other entities that control, are controlled by, or are under common
+ control with that entity. For the purposes of this definition,
+ "control" means (i) the power, direct or indirect, to cause the
+ direction or management of such entity, whether by contract or
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
+ outstanding shares, or (iii) beneficial ownership of such entity.
+
+ "You" (or "Your") shall mean an individual or Legal Entity
+ exercising permissions granted by this License.
+
+ "Source" form shall mean the preferred form for making modifications,
+ including but not limited to software source code, documentation
+ source, and configuration files.
+
+ "Object" form shall mean any form resulting from mechanical
+ transformation or translation of a Source form, including but
+ not limited to compiled object code, generated documentation,
+ and conversions to other media types.
+
+ "Work" shall mean the work of authorship, whether in Source or
+ Object form, made available under the License, as indicated by a
+ copyright notice that is included in or attached to the work
+ (an example is provided in the Appendix below).
+
+ "Derivative Works" shall mean any work, whether in Source or Object
+ form, that is based on (or derived from) the Work and for which the
+ editorial revisions, annotations, elaborations, or other modifications
+ represent, as a whole, an original work of authorship. For the purposes
+ of this License, Derivative Works shall not include works that remain
+ separable from, or merely link (or bind by name) to the interfaces of,
+ the Work and Derivative Works thereof.
+
+ "Contribution" shall mean any work of authorship, including
+ the original version of the Work and any modifications or additions
+ to that Work or Derivative Works thereof, that is intentionally
+ submitted to Licensor for inclusion in the Work by the copyright owner
+ or by an individual or Legal Entity authorized to submit on behalf of
+ the copyright owner. For the purposes of this definition, "submitted"
+ means any form of electronic, verbal, or written communication sent
+ to the Licensor or its representatives, including but not limited to
+ communication on electronic mailing lists, source code control systems,
+ and issue tracking systems that are managed by, or on behalf of, the
+ Licensor for the purpose of discussing and improving the Work, but
+ excluding communication that is conspicuously marked or otherwise
+ designated in writing by the copyright owner as "Not a Contribution."
+
+ "Contributor" shall mean Licensor and any individual or Legal Entity
+ on behalf of whom a Contribution has been received by Licensor and
+ subsequently incorporated within the Work.
+
+ 2. Grant of Copyright License. Subject to the terms and conditions of
+ this License, each Contributor hereby grants to You a perpetual,
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
+ copyright license to reproduce, prepare Derivative Works of,
+ publicly display, publicly perform, sublicense, and distribute the
+ Work and such Derivative Works in Source or Object form.
+
+ 3. Grant of Patent License. Subject to the terms and conditions of
+ this License, each Contributor hereby grants to You a perpetual,
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
+ (except as stated in this section) patent license to make, have made,
+ use, offer to sell, sell, import, and otherwise transfer the Work,
+ where such license applies only to those patent claims licensable
+ by such Contributor that are necessarily infringed by their
+ Contribution(s) alone or by combination of their Contribution(s)
+ with the Work to which such Contribution(s) was submitted. If You
+ institute patent litigation against any entity (including a
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
+ or a Contribution incorporated within the Work constitutes direct
+ or contributory patent infringement, then any patent licenses
+ granted to You under this License for that Work shall terminate
+ as of the date such litigation is filed.
+
+ 4. Redistribution. You may reproduce and distribute copies of the
+ Work or Derivative Works thereof in any medium, with or without
+ modifications, and in Source or Object form, provided that You
+ meet the following conditions:
+
+ (a) You must give any other recipients of the Work or
+ Derivative Works a copy of this License; and
+
+ (b) You must cause any modified files to carry prominent notices
+ stating that You changed the files; and
+
+ (c) You must retain, in the Source form of any Derivative Works
+ that You distribute, all copyright, patent, trademark, and
+ attribution notices from the Source form of the Work,
+ excluding those notices that do not pertain to any part of
+ the Derivative Works; and
+
+ (d) If the Work includes a "NOTICE" text file as part of its
+ distribution, then any Derivative Works that You distribute must
+ include a readable copy of the attribution notices contained
+ within such NOTICE file, excluding those notices that do not
+ pertain to any part of the Derivative Works, in at least one
+ of the following places: within a NOTICE text file distributed
+ as part of the Derivative Works; within the Source form or
+ documentation, if provided along with the Derivative Works; or,
+ within a display generated by the Derivative Works, if and
+ wherever such third-party notices normally appear. The contents
+ of the NOTICE file are for informational purposes only and
+ do not modify the License. You may add Your own attribution
+ notices within Derivative Works that You distribute, alongside
+ or as an addendum to the NOTICE text from the Work, provided
+ that such additional attribution notices cannot be construed
+ as modifying the License.
+
+ You may add Your own copyright statement to Your modifications and
+ may provide additional or different license terms and conditions
+ for use, reproduction, or distribution of Your modifications, or
+ for any such Derivative Works as a whole, provided Your use,
+ reproduction, and distribution of the Work otherwise complies with
+ the conditions stated in this License.
+
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
+ any Contribution intentionally submitted for inclusion in the Work
+ by You to the Licensor shall be under the terms and conditions of
+ this License, without any additional terms or conditions.
+ Notwithstanding the above, nothing herein shall supersede or modify
+ the terms of any separate license agreement you may have executed
+ with Licensor regarding such Contributions.
+
+ 6. Trademarks. This License does not grant permission to use the trade
+ names, trademarks, service marks, or product names of the Licensor,
+ except as required for reasonable and customary use in describing the
+ origin of the Work and reproducing the content of the NOTICE file.
+
+ 7. Disclaimer of Warranty. Unless required by applicable law or
+ agreed to in writing, Licensor provides the Work (and each
+ Contributor provides its Contributions) on an "AS IS" BASIS,
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
+ implied, including, without limitation, any warranties or conditions
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
+ PARTICULAR PURPOSE. You are solely responsible for determining the
+ appropriateness of using or redistributing the Work and assume any
+ risks associated with Your exercise of permissions under this License.
+
+ 8. Limitation of Liability. In no event and under no legal theory,
+ whether in tort (including negligence), contract, or otherwise,
+ unless required by applicable law (such as deliberate and grossly
+ negligent acts) or agreed to in writing, shall any Contributor be
+ liable to You for damages, including any direct, indirect, special,
+ incidental, or consequential damages of any character arising as a
+ result of this License or out of the use or inability to use the
+ Work (including but not limited to damages for loss of goodwill,
+ work stoppage, computer failure or malfunction, or any and all
+ other commercial damages or losses), even if such Contributor
+ has been advised of the possibility of such damages.
+
+ 9. Accepting Warranty or Additional Liability. While redistributing
+ the Work or Derivative Works thereof, You may choose to offer,
+ and charge a fee for, acceptance of support, warranty, indemnity,
+ or other liability obligations and/or rights consistent with this
+ License. However, in accepting such obligations, You may act only
+ on Your own behalf and on Your sole responsibility, not on behalf
+ of any other Contributor, and only if You agree to indemnify,
+ defend, and hold each Contributor harmless for any liability
+ incurred by, or claims asserted against, such Contributor by reason
+ of your accepting any such warranty or additional liability.
+
+ END OF TERMS AND CONDITIONS
+
+ APPENDIX: How to apply the Apache License to your work.
+
+ To apply the Apache License to your work, attach the following
+ boilerplate notice, with the fields enclosed by brackets "[]"
+ replaced with your own identifying information. (Don't include
+ the brackets!) The text should be enclosed in the appropriate
+ comment syntax for the file format. We also recommend that a
+ file or class name and description of purpose be included on the
+ same "printed page" as the copyright notice for easier
+ identification within third-party archives.
+
+ Copyright [yyyy] [name of copyright owner]
+
+ Licensed under the Apache License, Version 2.0 (the "License");
+ you may not use this file except in compliance with the License.
+ You may obtain a copy of the License at
+
+ http://www.apache.org/licenses/LICENSE-2.0
+
+ Unless required by applicable law or agreed to in writing, software
+ distributed under the License is distributed on an "AS IS" BASIS,
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ See the License for the specific language governing permissions and
+ limitations under the License.
diff --git a/README.md b/README.md
new file mode 100644
index 0000000..eb72f41
--- /dev/null
+++ b/README.md
@@ -0,0 +1,9 @@
+# Example app from Dex
+
+This repo contains the example app from the Dex repository (https://github.com/dexidp/dex/tree/master/examples/example-app).
+
+We did not make any modification on it;
+
+## License
+
+The project is licensed under the [Apache License, Version 2.0](LICENSE).
diff --git a/go.mod b/go.mod
new file mode 100644
index 0000000..b2ada2f
--- /dev/null
+++ b/go.mod
@@ -0,0 +1,19 @@
+module example
+
+go 1.20
+
+require (
+ github.com/coreos/go-oidc/v3 v3.9.0
+ github.com/spf13/cobra v1.8.0
+ golang.org/x/oauth2 v0.16.0
+)
+
+require (
+ github.com/go-jose/go-jose/v3 v3.0.1 // indirect
+ github.com/golang/protobuf v1.5.3 // indirect
+ github.com/inconshreveable/mousetrap v1.1.0 // indirect
+ github.com/spf13/pflag v1.0.5 // indirect
+ golang.org/x/crypto v0.14.0 // indirect
+ google.golang.org/appengine v1.6.8 // indirect
+ google.golang.org/protobuf v1.31.0 // indirect
+)
diff --git a/go.sum b/go.sum
new file mode 100644
index 0000000..064e14a
--- /dev/null
+++ b/go.sum
@@ -0,0 +1,68 @@
+github.com/coreos/go-oidc/v3 v3.9.0 h1:0J/ogVOd4y8P0f0xUh8l9t07xRP/d8tccvjHl2dcsSo=
+github.com/coreos/go-oidc/v3 v3.9.0/go.mod h1:rTKz2PYwftcrtoCzV5g5kvfJoWcm0Mk8AF8y1iAQro4=
+github.com/cpuguy83/go-md2man/v2 v2.0.3/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
+github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
+github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
+github.com/go-jose/go-jose/v3 v3.0.1 h1:pWmKFVtt+Jl0vBZTIpz/eAKwsm6LkIxDVVbFHKkchhA=
+github.com/go-jose/go-jose/v3 v3.0.1/go.mod h1:RNkWWRld676jZEYoV3+XK8L2ZnNSvIsxFMht0mSX+u8=
+github.com/golang/protobuf v1.5.0/go.mod h1:FsONVRAS9T7sI+LIUmWTfcYkHO4aIWwzhcaSAoJOfIk=
+github.com/golang/protobuf v1.5.2/go.mod h1:XVQd3VNwM+JqD3oG2Ue2ip4fOMUkwXdXDdiuN0vRsmY=
+github.com/golang/protobuf v1.5.3 h1:KhyjKVUg7Usr/dYsdSqoFveMYd5ko72D+zANwlG1mmg=
+github.com/golang/protobuf v1.5.3/go.mod h1:XVQd3VNwM+JqD3oG2Ue2ip4fOMUkwXdXDdiuN0vRsmY=
+github.com/google/go-cmp v0.5.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
+github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
+github.com/google/go-cmp v0.5.9 h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38=
+github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2s0bqwp9tc8=
+github.com/inconshreveable/mousetrap v1.1.0/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw=
+github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
+github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
+github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
+github.com/spf13/cobra v1.8.0 h1:7aJaZx1B85qltLMc546zn58BxxfZdR/W22ej9CFoEf0=
+github.com/spf13/cobra v1.8.0/go.mod h1:WXLWApfZ71AjXPya3WOlMsY9yMs7YeiHhFVlvLyhcho=
+github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA=
+github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
+github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
+github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
+github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY=
+github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY=
+golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
+golang.org/x/crypto v0.0.0-20190911031432-227b76d455e7/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
+golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
+golang.org/x/crypto v0.14.0 h1:wBqGXzWJW6m1XrIKlAH0Hs1JJ7+9KBwnIO8v66Q9cHc=
+golang.org/x/crypto v0.14.0/go.mod h1:MVFd36DqK4CsrnJYDkBA3VC4m2GkXAM0PvzMCn4JQf4=
+golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4=
+golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
+golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
+golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg=
+golang.org/x/net v0.0.0-20220722155237-a158d28d115b/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c=
+golang.org/x/oauth2 v0.16.0 h1:aDkGMBSYxElaoP81NpoUoz2oo2R2wHdZpGToUxfyQrQ=
+golang.org/x/oauth2 v0.16.0/go.mod h1:hqZ+0LWXsiVoZpeld6jVt06P3adbS2Uu911W1SsJv2o=
+golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
+golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
+golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
+golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
+golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
+golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
+golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
+golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
+golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
+golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
+golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
+golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
+golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ=
+golang.org/x/text v0.3.8/go.mod h1:E6s5w1FMmriuDzIBO73fBruAKo1PCIq6d2Q6DHfQ8WQ=
+golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
+golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
+golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc=
+golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
+golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
+google.golang.org/appengine v1.6.8 h1:IhEN5q69dyKagZPYMSdIjS2HqprW324FRQZJcGqPAsM=
+google.golang.org/appengine v1.6.8/go.mod h1:1jJ3jBArFh5pcgW8gCtRJnepW8FzD1V44FJffLiz/Ds=
+google.golang.org/protobuf v1.26.0-rc.1/go.mod h1:jlhhOSvTdKEhbULTjvd4ARK9grFBp09yW+WbY/TyQbw=
+google.golang.org/protobuf v1.26.0/go.mod h1:9q0QmTI4eRPtz6boOQmLYwt+qCgq0jsYwAQnmE0givc=
+google.golang.org/protobuf v1.31.0 h1:g0LDEJHgrBl9N9r17Ru3sqWhkIx2NB67okBHPwC7hs8=
+google.golang.org/protobuf v1.31.0/go.mod h1:HV8QOd/L58Z+nl8r43ehVNZIU/HEI6OcFqwMG9pJV4I=
+gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
+gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
+gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
+gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
diff --git a/main.go b/main.go
new file mode 100644
index 0000000..451bea5
--- /dev/null
+++ b/main.go
@@ -0,0 +1,339 @@
+package main
+
+import (
+ "bytes"
+ "context"
+ "crypto/tls"
+ "crypto/x509"
+ "encoding/json"
+ "errors"
+ "fmt"
+ "log"
+ "net"
+ "net/http"
+ "net/http/httputil"
+ "net/url"
+ "os"
+ "strings"
+ "time"
+
+ "github.com/coreos/go-oidc/v3/oidc"
+ "github.com/spf13/cobra"
+ "golang.org/x/oauth2"
+)
+
+const exampleAppState = "I wish to wash my irish wristwatch"
+
+type app struct {
+ clientID string
+ clientSecret string
+ redirectURI string
+
+ verifier *oidc.IDTokenVerifier
+ provider *oidc.Provider
+
+ // Does the provider use "offline_access" scope to request a refresh token
+ // or does it use "access_type=offline" (e.g. Google)?
+ offlineAsScope bool
+
+ client *http.Client
+}
+
+// return an HTTP client which trusts the provided root CAs.
+func httpClientForRootCAs(rootCAs string) (*http.Client, error) {
+ tlsConfig := tls.Config{RootCAs: x509.NewCertPool()}
+ rootCABytes, err := os.ReadFile(rootCAs)
+ if err != nil {
+ return nil, fmt.Errorf("failed to read root-ca: %v", err)
+ }
+ if !tlsConfig.RootCAs.AppendCertsFromPEM(rootCABytes) {
+ return nil, fmt.Errorf("no certs found in root CA file %q", rootCAs)
+ }
+ return &http.Client{
+ Transport: &http.Transport{
+ TLSClientConfig: &tlsConfig,
+ Proxy: http.ProxyFromEnvironment,
+ Dial: (&net.Dialer{
+ Timeout: 30 * time.Second,
+ KeepAlive: 30 * time.Second,
+ }).Dial,
+ TLSHandshakeTimeout: 10 * time.Second,
+ ExpectContinueTimeout: 1 * time.Second,
+ },
+ }, nil
+}
+
+type debugTransport struct {
+ t http.RoundTripper
+}
+
+func (d debugTransport) RoundTrip(req *http.Request) (*http.Response, error) {
+ reqDump, err := httputil.DumpRequest(req, true)
+ if err != nil {
+ return nil, err
+ }
+ log.Printf("%s", reqDump)
+
+ resp, err := d.t.RoundTrip(req)
+ if err != nil {
+ return nil, err
+ }
+
+ respDump, err := httputil.DumpResponse(resp, true)
+ if err != nil {
+ resp.Body.Close()
+ return nil, err
+ }
+ log.Printf("%s", respDump)
+ return resp, nil
+}
+
+func cmd() *cobra.Command {
+ var (
+ a app
+ issuerURL string
+ listen string
+ tlsCert string
+ tlsKey string
+ rootCAs string
+ debug bool
+ )
+ c := cobra.Command{
+ Use: "example-app",
+ Short: "An example OpenID Connect client",
+ Long: "",
+ RunE: func(cmd *cobra.Command, args []string) error {
+ if len(args) != 0 {
+ return errors.New("surplus arguments provided")
+ }
+
+ u, err := url.Parse(a.redirectURI)
+ if err != nil {
+ return fmt.Errorf("parse redirect-uri: %v", err)
+ }
+ listenURL, err := url.Parse(listen)
+ if err != nil {
+ return fmt.Errorf("parse listen address: %v", err)
+ }
+
+ if rootCAs != "" {
+ client, err := httpClientForRootCAs(rootCAs)
+ if err != nil {
+ return err
+ }
+ a.client = client
+ }
+
+ if debug {
+ if a.client == nil {
+ a.client = &http.Client{
+ Transport: debugTransport{http.DefaultTransport},
+ }
+ } else {
+ a.client.Transport = debugTransport{a.client.Transport}
+ }
+ }
+
+ if a.client == nil {
+ a.client = http.DefaultClient
+ }
+
+ // TODO(ericchiang): Retry with backoff
+ ctx := oidc.ClientContext(context.Background(), a.client)
+ provider, err := oidc.NewProvider(ctx, issuerURL)
+ if err != nil {
+ return fmt.Errorf("failed to query provider %q: %v", issuerURL, err)
+ }
+
+ var s struct {
+ // What scopes does a provider support?
+ //
+ // See: https://openid.net/specs/openid-connect-discovery-1_0.html#ProviderMetadata
+ ScopesSupported []string `json:"scopes_supported"`
+ }
+ if err := provider.Claims(&s); err != nil {
+ return fmt.Errorf("failed to parse provider scopes_supported: %v", err)
+ }
+
+ if len(s.ScopesSupported) == 0 {
+ // scopes_supported is a "RECOMMENDED" discovery claim, not a required
+ // one. If missing, assume that the provider follows the spec and has
+ // an "offline_access" scope.
+ a.offlineAsScope = true
+ } else {
+ // See if scopes_supported has the "offline_access" scope.
+ a.offlineAsScope = func() bool {
+ for _, scope := range s.ScopesSupported {
+ if scope == oidc.ScopeOfflineAccess {
+ return true
+ }
+ }
+ return false
+ }()
+ }
+
+ a.provider = provider
+ a.verifier = provider.Verifier(&oidc.Config{ClientID: a.clientID})
+
+ http.HandleFunc("/", a.handleIndex)
+ http.HandleFunc("/login", a.handleLogin)
+ http.HandleFunc(u.Path, a.handleCallback)
+
+ switch listenURL.Scheme {
+ case "http":
+ log.Printf("listening on %s", listen)
+ return http.ListenAndServe(listenURL.Host, nil)
+ case "https":
+ log.Printf("listening on %s", listen)
+ return http.ListenAndServeTLS(listenURL.Host, tlsCert, tlsKey, nil)
+ default:
+ return fmt.Errorf("listen address %q is not using http or https", listen)
+ }
+ },
+ }
+ c.Flags().StringVar(&a.clientID, "client-id", "example-app", "OAuth2 client ID of this application.")
+ c.Flags().StringVar(&a.clientSecret, "client-secret", "ZXhhbXBsZS1hcHAtc2VjcmV0", "OAuth2 client secret of this application.")
+ c.Flags().StringVar(&a.redirectURI, "redirect-uri", "http://127.0.0.1:5555/callback", "Callback URL for OAuth2 responses.")
+ c.Flags().StringVar(&issuerURL, "issuer", "http://127.0.0.1:5556/dex", "URL of the OpenID Connect issuer.")
+ c.Flags().StringVar(&listen, "listen", "http://127.0.0.1:5555", "HTTP(S) address to listen at.")
+ c.Flags().StringVar(&tlsCert, "tls-cert", "", "X509 cert file to present when serving HTTPS.")
+ c.Flags().StringVar(&tlsKey, "tls-key", "", "Private key for the HTTPS cert.")
+ c.Flags().StringVar(&rootCAs, "issuer-root-ca", "", "Root certificate authorities for the issuer. Defaults to host certs.")
+ c.Flags().BoolVar(&debug, "debug", false, "Print all request and responses from the OpenID Connect issuer.")
+ return &c
+}
+
+func main() {
+ if err := cmd().Execute(); err != nil {
+ fmt.Fprintf(os.Stderr, "error: %v\n", err)
+ os.Exit(2)
+ }
+}
+
+func (a *app) handleIndex(w http.ResponseWriter, r *http.Request) {
+ renderIndex(w)
+}
+
+func (a *app) oauth2Config(scopes []string) *oauth2.Config {
+ return &oauth2.Config{
+ ClientID: a.clientID,
+ ClientSecret: a.clientSecret,
+ Endpoint: a.provider.Endpoint(),
+ Scopes: scopes,
+ RedirectURL: a.redirectURI,
+ }
+}
+
+func (a *app) handleLogin(w http.ResponseWriter, r *http.Request) {
+ var scopes []string
+ if extraScopes := r.FormValue("extra_scopes"); extraScopes != "" {
+ scopes = strings.Split(extraScopes, " ")
+ }
+ var clients []string
+ if crossClients := r.FormValue("cross_client"); crossClients != "" {
+ clients = strings.Split(crossClients, " ")
+ }
+ for _, client := range clients {
+ scopes = append(scopes, "audience:server:client_id:"+client)
+ }
+ connectorID := ""
+ if id := r.FormValue("connector_id"); id != "" {
+ connectorID = id
+ }
+
+ authCodeURL := ""
+ scopes = append(scopes, "openid", "profile", "email")
+ if r.FormValue("offline_access") != "yes" {
+ authCodeURL = a.oauth2Config(scopes).AuthCodeURL(exampleAppState)
+ } else if a.offlineAsScope {
+ scopes = append(scopes, "offline_access")
+ authCodeURL = a.oauth2Config(scopes).AuthCodeURL(exampleAppState)
+ } else {
+ authCodeURL = a.oauth2Config(scopes).AuthCodeURL(exampleAppState, oauth2.AccessTypeOffline)
+ }
+ if connectorID != "" {
+ authCodeURL = authCodeURL + "&connector_id=" + connectorID
+ }
+
+ http.Redirect(w, r, authCodeURL, http.StatusSeeOther)
+}
+
+func (a *app) handleCallback(w http.ResponseWriter, r *http.Request) {
+ var (
+ err error
+ token *oauth2.Token
+ )
+
+ ctx := oidc.ClientContext(r.Context(), a.client)
+ oauth2Config := a.oauth2Config(nil)
+ switch r.Method {
+ case http.MethodGet:
+ // Authorization redirect callback from OAuth2 auth flow.
+ if errMsg := r.FormValue("error"); errMsg != "" {
+ http.Error(w, errMsg+": "+r.FormValue("error_description"), http.StatusBadRequest)
+ return
+ }
+ code := r.FormValue("code")
+ if code == "" {
+ http.Error(w, fmt.Sprintf("no code in request: %q", r.Form), http.StatusBadRequest)
+ return
+ }
+ if state := r.FormValue("state"); state != exampleAppState {
+ http.Error(w, fmt.Sprintf("expected state %q got %q", exampleAppState, state), http.StatusBadRequest)
+ return
+ }
+ token, err = oauth2Config.Exchange(ctx, code)
+ case http.MethodPost:
+ // Form request from frontend to refresh a token.
+ refresh := r.FormValue("refresh_token")
+ if refresh == "" {
+ http.Error(w, fmt.Sprintf("no refresh_token in request: %q", r.Form), http.StatusBadRequest)
+ return
+ }
+ t := &oauth2.Token{
+ RefreshToken: refresh,
+ Expiry: time.Now().Add(-time.Hour),
+ }
+ token, err = oauth2Config.TokenSource(ctx, t).Token()
+ default:
+ http.Error(w, fmt.Sprintf("method not implemented: %s", r.Method), http.StatusBadRequest)
+ return
+ }
+
+ if err != nil {
+ http.Error(w, fmt.Sprintf("failed to get token: %v", err), http.StatusInternalServerError)
+ return
+ }
+
+ rawIDToken, ok := token.Extra("id_token").(string)
+ if !ok {
+ http.Error(w, "no id_token in token response", http.StatusInternalServerError)
+ return
+ }
+
+ idToken, err := a.verifier.Verify(r.Context(), rawIDToken)
+ if err != nil {
+ http.Error(w, fmt.Sprintf("failed to verify ID token: %v", err), http.StatusInternalServerError)
+ return
+ }
+
+ accessToken, ok := token.Extra("access_token").(string)
+ if !ok {
+ http.Error(w, "no access_token in token response", http.StatusInternalServerError)
+ return
+ }
+
+ var claims json.RawMessage
+ if err := idToken.Claims(&claims); err != nil {
+ http.Error(w, fmt.Sprintf("error decoding ID token claims: %v", err), http.StatusInternalServerError)
+ return
+ }
+
+ buff := new(bytes.Buffer)
+ if err := json.Indent(buff, []byte(claims), "", " "); err != nil {
+ http.Error(w, fmt.Sprintf("error indenting ID token claims: %v", err), http.StatusInternalServerError)
+ return
+ }
+
+ renderToken(w, a.redirectURI, rawIDToken, accessToken, token.RefreshToken, buff.String())
+}
diff --git a/templates.go b/templates.go
new file mode 100644
index 0000000..a9425ea
--- /dev/null
+++ b/templates.go
@@ -0,0 +1,110 @@
+package main
+
+import (
+ "html/template"
+ "log"
+ "net/http"
+)
+
+var indexTmpl = template.Must(template.New("index.html").Parse(`
+
+
+
+
+
+
+`))
+
+func renderIndex(w http.ResponseWriter) {
+ renderTemplate(w, indexTmpl, nil)
+}
+
+type tokenTmplData struct {
+ IDToken string
+ AccessToken string
+ RefreshToken string
+ RedirectURL string
+ Claims string
+}
+
+var tokenTmpl = template.Must(template.New("token.html").Parse(`
+
+
+
+
+ ID Token:
{{ .IDToken }}
+ Access Token:
{{ .AccessToken }}
+ Claims:
{{ .Claims }}
+ {{ if .RefreshToken }}
+ Refresh Token:
{{ .RefreshToken }}
+
+ {{ end }}
+
+
+`))
+
+func renderToken(w http.ResponseWriter, redirectURL, idToken, accessToken, refreshToken, claims string) {
+ renderTemplate(w, tokenTmpl, tokenTmplData{
+ IDToken: idToken,
+ AccessToken: accessToken,
+ RefreshToken: refreshToken,
+ RedirectURL: redirectURL,
+ Claims: claims,
+ })
+}
+
+func renderTemplate(w http.ResponseWriter, tmpl *template.Template, data interface{}) {
+ err := tmpl.Execute(w, data)
+ if err == nil {
+ return
+ }
+
+ switch err := err.(type) {
+ case *template.Error:
+ // An ExecError guarantees that Execute has not written to the underlying reader.
+ log.Printf("Error rendering template %s: %s", tmpl.Name(), err)
+
+ // TODO(ericchiang): replace with better internal server error.
+ http.Error(w, "Internal server error", http.StatusInternalServerError)
+ default:
+ // An error with the underlying write, such as the connection being
+ // dropped. Ignore for now.
+ }
+}