Skip to content

Instantly share code, notes, and snippets.

@tuxlinuxien
Last active October 18, 2023 10:50
Show Gist options
  • Star 7 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save tuxlinuxien/29aa0ddaaa33c506c6220266bbb632a1 to your computer and use it in GitHub Desktop.
Save tuxlinuxien/29aa0ddaaa33c506c6220266bbb632a1 to your computer and use it in GitHub Desktop.
gin gonic oauth2 integration
package main
import (
"bytes"
"encoding/json"
"github.com/gin-gonic/gin"
"golang.org/x/oauth2"
"golang.org/x/oauth2/bitbucket"
"golang.org/x/oauth2/github"
"golang.org/x/oauth2/google"
"golang.org/x/oauth2/linkedin"
"io/ioutil"
"log"
)
var (
GoogleAuth *oauth2.Config = &oauth2.Config{
ClientID: "",
ClientSecret: "",
RedirectURL: "http://localhost:8080/account/google/callback",
Scopes: []string{
"https://www.googleapis.com/auth/userinfo.email",
},
Endpoint: google.Endpoint,
}
GithubAuth *oauth2.Config = &oauth2.Config{
ClientID: "",
ClientSecret: "",
RedirectURL: "http://localhost:8080/account/github/callback",
Scopes: []string{
"user:email",
},
Endpoint: github.Endpoint,
}
BitbucketAuth *oauth2.Config = &oauth2.Config{
ClientID: "",
ClientSecret: "",
RedirectURL: "http://localhost:8080/account/bitbucket/callback",
Scopes: []string{
"email",
},
Endpoint: bitbucket.Endpoint,
}
LinkedinAuth *oauth2.Config = &oauth2.Config{
ClientID: "",
ClientSecret: "",
RedirectURL: "http://localhost:8080/account/linkedin/callback",
Scopes: []string{
"r_basicprofile",
"r_emailaddress",
},
Endpoint: linkedin.Endpoint,
}
)
// google
func gInit(c *gin.Context) {
url := GoogleAuth.AuthCodeURL("state", oauth2.AccessTypeOffline)
c.Redirect(302, url)
}
func gCallback(c *gin.Context) {
code := c.Query("code")
tok, err := GoogleAuth.Exchange(oauth2.NoContext, code)
if err != nil {
log.Fatal(err)
}
client := GoogleAuth.Client(oauth2.NoContext, tok)
resp, err := client.Get("https://www.googleapis.com/plus/v1/people/me")
if err != nil {
log.Fatal(err)
}
defer resp.Body.Close()
body, _ := ioutil.ReadAll(resp.Body)
log.Println("body", string(body))
var prettyJSON bytes.Buffer
err = json.Indent(&prettyJSON, body, "", "\t")
if err != nil {
log.Println("JSON parse error: ", err)
return
}
c.String(200, string(prettyJSON.Bytes()))
}
// github
func ghInit(c *gin.Context) {
url := GithubAuth.AuthCodeURL("state", oauth2.AccessTypeOffline)
c.Redirect(302, url)
}
func ghCallback(c *gin.Context) {
code := c.Query("code")
tok, err := GithubAuth.Exchange(oauth2.NoContext, code)
if err != nil {
log.Fatal(err)
}
client := GithubAuth.Client(oauth2.NoContext, tok)
resp, err := client.Get("https://api.github.com/user")
if err != nil {
log.Fatal(err)
}
defer resp.Body.Close()
body, _ := ioutil.ReadAll(resp.Body)
log.Println("body", string(body))
var prettyJSON bytes.Buffer
err = json.Indent(&prettyJSON, body, "", "\t")
if err != nil {
log.Println("JSON parse error: ", err)
return
}
c.String(200, string(prettyJSON.Bytes()))
}
// bitbucket
func bbInit(c *gin.Context) {
url := BitbucketAuth.AuthCodeURL("state", oauth2.AccessTypeOffline)
c.Redirect(302, url)
}
func bbCallback(c *gin.Context) {
code := c.Query("code")
tok, err := BitbucketAuth.Exchange(oauth2.NoContext, code)
if err != nil {
log.Fatal(err)
}
client := BitbucketAuth.Client(oauth2.NoContext, tok)
resp, err := client.Get("https://api.bitbucket.org/2.0/user")
if err != nil {
log.Fatal(err)
}
defer resp.Body.Close()
body, _ := ioutil.ReadAll(resp.Body)
log.Println("body", string(body))
var prettyJSON bytes.Buffer
err = json.Indent(&prettyJSON, body, "", "\t")
if err != nil {
log.Println("JSON parse error: ", err)
return
}
c.String(200, string(prettyJSON.Bytes()))
}
// linkedin
func lInit(c *gin.Context) {
url := LinkedinAuth.AuthCodeURL("state", oauth2.AccessTypeOffline)
c.Redirect(302, url)
}
func lCallback(c *gin.Context) {
code := c.Query("code")
tok, err := LinkedinAuth.Exchange(oauth2.NoContext, code)
if err != nil {
log.Fatal(err)
}
client := LinkedinAuth.Client(oauth2.NoContext, tok)
resp, err := client.Get("https://api.linkedin.com/v1/people/~:(id,first-name,last-name,email-address)?format=json")
if err != nil {
log.Fatal(err)
}
defer resp.Body.Close()
body, _ := ioutil.ReadAll(resp.Body)
log.Println("body", string(body))
var prettyJSON bytes.Buffer
err = json.Indent(&prettyJSON, body, "", "\t")
if err != nil {
log.Println("JSON parse error: ", err)
return
}
c.String(200, string(prettyJSON.Bytes()))
}
func home(c *gin.Context) {
page := `<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
</head>
<body>
<a href="/link/google">Google+</a>
<br>
<a href="/link/github">Github</a>
<br>
<a href="/link/bitbucket">Bitbucket</a>
<br>
<a href="/link/linkedin">Linkedin</a>
</body>
</html>
`
c.String(200, page)
}
func main() {
router := gin.New()
router.Use(gin.Recovery())
router.GET("/")
router.GET("/link/google", gInit)
router.GET("/account/google/callback", gCallback)
router.GET("/link/github", ghInit)
router.GET("/account/github/callback", ghCallback)
router.GET("/link/bitbucket", bbInit)
router.GET("/account/bitbucket/callback", bbCallback)
router.GET("/link/linkedin", lInit)
router.GET("/account/linkedin/callback", lCallback)
router.Run(":8080")
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment