Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/kevinburke/google-oauth-handler
HTTP middleware for handling Google authentication
https://github.com/kevinburke/google-oauth-handler
golang google google-oauth-handler middleware oauth2
Last synced: 3 months ago
JSON representation
HTTP middleware for handling Google authentication
- Host: GitHub
- URL: https://github.com/kevinburke/google-oauth-handler
- Owner: kevinburke
- License: mit
- Created: 2017-04-07T20:13:14.000Z (over 7 years ago)
- Default Branch: master
- Last Pushed: 2019-08-27T06:14:38.000Z (over 5 years ago)
- Last Synced: 2024-06-21T18:51:07.754Z (6 months ago)
- Topics: golang, google, google-oauth-handler, middleware, oauth2
- Language: Go
- Size: 23.4 KB
- Stars: 5
- Watchers: 1
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# google-oauth-handler
Package `google_oauth_handler` transparently handles OAuth authentication with
Google.Create an Authenticator and then insert it as middleware in front of any
resources you want to protect behind Google login, via authenticator.Handle.
Handle will call the next middleware with (w, r, *Auth), which you can use
to make requests to the Google API.The Authenticator handles the OAuth workflow for you, redirecting users to
Google, handling the callback and setting an encrypted cookie in the user's
browser.For more information, see the [godoc documentation][godoc].
[godoc]: https://godoc.org/github.com/kevinburke/google-oauth-handler
### Example
```go
package google_oauth_handler_testimport (
"encoding/hex"
"fmt"
"net/http"google "github.com/kevinburke/google-oauth-handler"
"golang.org/x/oauth2"
)var key *[32]byte
func init() {
secretKeyBytes, _ := hex.DecodeString("982a732cc3d72d13678dee2609cf55d736711ff1f293f95cab41bd45e5d77870")
key = new([32]byte)
copy(key[:], secretKeyBytes)
}func Example() {
cfg := google.Config{
SecretKey: key,
BaseURL: "https://example.com",
ClientID: "customdomain.apps.googleusercontent.com",
Secret: "W-secretkey",
Scopes: []string{
"email",
"https://www.googleapis.com/auth/gmail.send",
},
}
auth := google.NewAuthenticator(cfg)
http.Handle("/", auth.Handle(func(w http.ResponseWriter, r *http.Request, auth *google.Auth) {
fmt.Fprintf(w, "Hello World
Token: %s
", auth.AccessToken)
}))
}
```