Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/adhocore/goic
Golang OpenID Connect Client
https://github.com/adhocore/goic
adhocore claims go-oidc golang google-authentication json jwt login-with-google login-with-microsoft login-with-yahoo oauth2 oidc oidc-client openid openid-client openid-connect
Last synced: 2 months ago
JSON representation
Golang OpenID Connect Client
- Host: GitHub
- URL: https://github.com/adhocore/goic
- Owner: adhocore
- License: mit
- Created: 2021-10-11T08:55:25.000Z (about 3 years ago)
- Default Branch: main
- Last Pushed: 2024-04-13T06:44:30.000Z (9 months ago)
- Last Synced: 2024-10-04T20:36:59.299Z (3 months ago)
- Topics: adhocore, claims, go-oidc, golang, google-authentication, json, jwt, login-with-google, login-with-microsoft, login-with-yahoo, oauth2, oidc, oidc-client, openid, openid-client, openid-connect
- Language: Go
- Homepage: https://github.com/adhocore/goic
- Size: 93.8 KB
- Stars: 30
- Watchers: 3
- Forks: 2
- Open Issues: 3
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- Funding: .github/FUNDING.yml
- License: LICENSE
Awesome Lists containing this project
README
# adhocore/goic
[![Latest Version](https://img.shields.io/github/release/adhocore/goic.svg?style=flat-square)](https://github.com/adhocore/goic/releases)
[![Software License](https://img.shields.io/badge/license-MIT-brightgreen.svg?style=flat-square)](LICENSE)
[![Go Report](https://goreportcard.com/badge/github.com/adhocore/goic)](https://goreportcard.com/report/github.com/adhocore/goic)
[![Tweet](https://img.shields.io/twitter/url/http/shields.io.svg?style=social)](https://twitter.com/intent/tweet?text=Simple+Golang+OpenID+Connect+client&url=https://github.com/adhocore/goic&hashtags=go,golang,openid,oauth,openid-connect,connect,oauth2)
[![Support](https://img.shields.io/static/v1?label=Support&message=%E2%9D%A4&logo=GitHub)](https://github.com/sponsors/adhocore)GOIC, **Go Open ID Connect**, is OpenID connect client library for Golang.
It supports the *Authorization Code Flow* of OpenID Connect specification.# Installation
```sh
go get -u github.com/adhocore/goic
```# Usage
Decide an endpoint (aka URI) in your server where you would like `goic` to intercept and add OpenID Connect flow.
Let's say `/auth/o8`. Then the provider name follows it.
All the OpenID providers that your server should support will need a unique name and each of the
providers get a URI like so `/auth/o8/`. Example:| Provider | Name | OpenID URI | Revocation | Signout |
|----------|------|------------|----------|------------|
| Google | google | `/auth/o8/google` | Yes | No
| Facebook | facebook | `/auth/o8/facebook` | No | No
| Microsoft | microsoft | `/auth/o8/microsoft` | No | Yes
| Yahoo | yahoo | `/auth/o8/yahoo` | Yes | No
| Paypal | paypal | `/auth/o8/paypal` | No | No**Important:** All the providers **must** provide .well-known configurations for OpenID auto discovery.
Get ready with OpenID provider credentials (client id and secret).
For Google, check [this](https://developers.google.com/identity/gsi/web/guides/get-google-api-clientid).
To use the example below you need to export `GOOGLE_CLIENT_ID` and `GOOGLE_CLIENT_SECRET` env vars.You also need to configure application domain and redirect URI in the Provider console/dashboard.
(redirect URI is same as OpenID URI in above table).Below is an example for authorization code flow but instead of copy/pasting it entirely you can use it for reference.
```go
package mainimport (
"log"
"net/http"
"os""github.com/adhocore/goic"
)func main() {
// Init GOIC with a root uri and verbose mode (=true)
g := goic.New("/auth/o8", true)// Register Google provider with name google and its auth URI
// It will preemptively load well-known config and jwks keys
p := g.NewProvider("google", "https://accounts.google.com")// Configure credentials for Google provider
p.WithCredential(os.Getenv("GOOGLE_CLIENT_ID"), os.Getenv("GOOGLE_CLIENT_SECRET"))// Configure scope
p.WithScope("openid email profile")// Define a callback that will receive token and user info on successful verification
g.UserCallback(func(t *goic.Token, u *goic.User, w http.ResponseWriter, r *http.Request) {
// Persist token and user info as you wish! Be sure to check for error in `u.Error` first
// Use the available `w` and `r` params to show some nice page with message to your user
// OR redirect them to homepage/dashboard etc// However, for the example, here I just dump it in backend console
log.Println("token: ", t)
log.Println("user: ", u)// and tell the user it is all good:
_, _ = w.Write([]byte("All good, check backend console"))
})// Listen address for server, 443 for https as OpenID connect mandates it!
addr := "localhost:443"
// You need to find a way to run your localhost in HTTPS as well.
// You may also alias it something like `goic.lvh.me` (lvh is local virtual host)
// *.lvh.me is automatically mapped to 127.0.0.1 in unix systems.// A catch-all dummy handler
handler := func(w http.ResponseWriter, r *http.Request) {
_, _ = w.Write([]byte(r.Method + " " + r.URL.Path))
}log.Println("Server running on https://localhost")
log.Println(" Visit https://localhost/auth/o8/google")// This is just example (don't copy it)
useMux := os.Getenv("GOIC_HTTP_MUX") == "1"
if useMux {
mux := http.NewServeMux()
// If you use http mux, wrap your handler with g.MiddlewareHandler
mux.Handle("/", g.MiddlewareHandler(http.HandlerFunc(handler)))
server := &http.Server{Addr: addr, Handler: mux}
log.Fatal(server.ListenAndServeTLS("server.crt", "server.key"))
} else {
// If you just use plain simple handler func,
// wrap your handler with g.MiddlewareFunc
http.HandleFunc("/", g.MiddlewareFunc(handler))
log.Fatal(http.ListenAndServeTLS(addr, "server.crt", "server.key", nil))
}
}
``````go
// OR, you can use shorthand syntax to register providers:g := goic.New("/auth/o8", false)
g.AddProvider(goic.Google.WithCredential(os.Getenv("GOOGLE_CLIENT_ID"), os.Getenv("GOOGLE_CLIENT_SECRET")))
g.AddProvider(goic.Microsoft.WithCredential(os.Getenv("MICROSOFT_CLIENT_ID"), os.Getenv("MICROSOFT_CLIENT_SECRET")))
g.AddProvider(goic.Yahoo.WithCredential(os.Getenv("YAHOO_CLIENT_ID"), os.Getenv("YAHOO_CLIENT_SECRET")))// ...
```After having code like that, build the binary (`go build`) and run server program (`./`).
You need to point `Sign in with ` button to `https://localhost/auth/o8/` for your end user.
For example:
```html
Sign in with Google
Sign in with Microsoft
Sign in with Yahoo
```The complete flow is managed and handled by GOIC for you and on successful verification,
You will be able to receive user and token info in your callback via `g.UserCallback`!
That is where you persist the user data, set some cookie etc.Check [examples](./examples) directory later for more, as it will be updated
when GOIC has new features.> The example and discussion here assume `localhost` domain so adjust that accordingly for your domains.
### Signing out
For signing out you need to manually invoke `g.SignOut()` from within http context. See the [API](#signout) below.
There is also a working [example](./examples/all.go). Note that not all Providers support signing out.### Revocation
To revoke a token manually, invoke `g.RevokeToken()` from any context. See the [API](#revoketoken) below.
There is also a working [example](./examples/all.go). Note that not all Providers support revocation.---
## GOIC APIGOIC supports full end-to-end for Authorization Code Flow, however if you want to manually interact, here's summary of API:
#### Supports
Use it to check if a provider is supported.
```go
g := goic.New("/auth/o8", false)
g.NewProvider("abc", "...").WithCredential("...", "...")g.Supports("abc") // true
g.Supports("xyz") // false
```#### RequestAuth
Manually request authentication from OpenID Provider. Must be called from within http context.
```go
g := goic.New("/auth/o8", false)
p := g.NewProvider("abc", "...").WithCredential("...", "...")// Generate random unique state and nonce
state, nonce := goic.RandomString(24), goic.RandomString(24)
// You must save them to cookie/session, so it can be retrieved later for crosscheck// redir is the redirect url in your host for provider of interest
redir := "https://localhost/auth/o8/" + p.Name// Redirects to provider first and then back to above redir url
// res = http.ResponseWriter, req = *http.Request
err := g.RequestAuth(p, state, nonce, redir, res, req)
```#### Authenticate
Manually attempt to authenticate after the request comes back from OpenID Provider.
```go
g := goic.New("/auth/o8", false)
p := g.NewProvider("abc", "...").WithCredential("...", "...")// Read openid provider code from query param, and nonce from cookie/session etc
// PS: Validate that the nonce is relevant to the state sent by openid provider
code, nonce := "", ""// redir is the redirect url in your host for provider of interest
redir := "https://localhost/auth/o8/" + p.Nametok, err := g.Authenticate(p, code, nonce, redir)
```#### RefreshToken
Use it to request Access token by using refresh token.
```go
g := goic.New("/auth/o8", false)
// ... add providers
old := &goic.Token{RefreshToken: "your refresh token", Provider: goic.Microsoft.Name}
tok, err := g.RefreshToken(old)
// Do something with new tok.AccessToken
```#### Userinfo
Manually request Userinfo by using the token returned by Authentication above.
```go
g := goic.New("/auth/o8", false)
p := g.NewProvider("abc", "...").WithCredential("...", "...")
// ...
tok, err := g.Authenticate(p, code, nonce, redir)
user := g.UserInfo(tok)
err := user.Error
```#### SignOut
Use it to sign out the user from OpenID Provider. Must be called from within http context.
Ideally, you would clear the session and logout user from your own system first and then invoke SignOut.```go
g := goic.New("/auth/o8", false)
p := g.NewProvider("abc", "...").WithCredential("...", "...")
// ...
tok := &goic.Token{AccessToken: "current session token", Provider: p.Name}
err := g.SignOut(tok, "http://some/preconfigured/redir/uri", res, req)
// redir uri is optional
err := g.SignOut(tok, "", res, req)
```#### RevokeToken
Use it to revoke the token so that is incapacitated.
```go
g := goic.New("/auth/o8", false)
p := g.NewProvider("abc", "...").WithCredential("...", "...")
// ...
tok := &goic.Token{AccessToken: "current session token", Provider: p.Name}
err := g.RevokeToken(tok)
```---
### Demo`GOIC` has been implemented in opensource project [adhocore/urlsh](https://github.com/adhocore/urlsh):
| Provider | Name | Demo URL |
|----------|------|------------|
| Google | google | [urlssh.xyz/auth/o8/google](https://urlssh.xyz/auth/o8/google) |
| Microsoft | microsoft | [urlssh.xyz/auth/o8/microsoft](https://urlssh.xyz/auth/o8/microsoft) |
| Yahoo | yahoo | [urlssh.xyz/auth/o8/yahoo](https://urlssh.xyz/auth/o8/yahoo) |On successful verification your information is [echoed back](https://github.com/adhocore/urlsh/blob/main/router/router.go#L48-L53) to you as JSON but **not** saved in server (pinky promise).
---
# TODO- [X] ~~Support refresh token grant_type~~ [Check #2](https://github.com/adhocore/goic/issues/2)
- [ ] Tests and more tests
- [ ] Release stable version
- [x] ~~Support OpenID `Implicit Flow`~~ [Check #3](https://github.com/adhocore/goic/issues/3)## License
> © [MIT](./LICENSE) | 2021-2099, Jitendra Adhikari
## Credits
Release managed by [please](https://github.com/adhocore/please).
---
### Other projects
My other golang projects you might find interesting and useful:- [**gronx**](https://github.com/adhocore/gronx) - Lightweight, fast and dependency-free Cron expression parser (due checker), task scheduler and/or daemon for Golang (tested on v1.13 and above) and standalone usage.
- [**urlsh**](https://github.com/adhocore/urlsh) - URL shortener and bookmarker service with UI, API, Cache, Hits Counter and forwarder using postgres and redis in backend, bulma in frontend; has [web](https://urlssh.xyz) and cli client
- [**fast**](https://github.com/adhocore/fast) - Check your internet speed with ease and comfort right from the terminal
- [**chin**](https://github.com/adhocore/chin) - A GO lang command line tool to show a spinner as user waits for some long running jobs to finish.