shared project
This commit is contained in:
commit
c676253e85
60
main.go
Normal file
60
main.go
Normal file
|
@ -0,0 +1,60 @@
|
|||
package main
|
||||
|
||||
|
||||
import (
|
||||
"github.com/gin-gonic/gin"
|
||||
"golang.org/x/oauth2"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"golang.org/x/oauth2/facebook"
|
||||
"golang.org/x/oauth2/google"
|
||||
)
|
||||
|
||||
var config *oauth2.Config
|
||||
|
||||
func getConfig(snsType SNSType) *oauth2.Config {
|
||||
|
||||
if snsType == FB_TYPE {
|
||||
return &oauth2.Config{
|
||||
ClientID:FB_CLIENT_ID,
|
||||
ClientSecret:FB_CLIENT_SECRET,
|
||||
RedirectURL:REDIRECT_URL,
|
||||
Scopes:[]string{"email","public_profile"},
|
||||
Endpoint:facebook.Endpoint,
|
||||
}
|
||||
} else if snsType == GG_TYPE {
|
||||
|
||||
return &oauth2.Config{
|
||||
ClientID:GG_CLIENT_ID,
|
||||
ClientSecret:GG_CLIENT_SECRET,
|
||||
RedirectURL:REDIRECT_URL,
|
||||
Scopes:[]string{"https://www.googleapis.com/auth/userinfo.email"},
|
||||
Endpoint:google.Endpoint,
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
func Home(c *gin.Context) {
|
||||
config = getConfig(GG_TYPE)
|
||||
codeUrl := config.AuthCodeURL("")
|
||||
http.Redirect(c.Writer, c.Request, codeUrl,http.StatusTemporaryRedirect)
|
||||
|
||||
}
|
||||
|
||||
func SNSCallback(c *gin.Context) {
|
||||
c.Writer.Header().Set("Content-Type", "text/html; charset=utf-8")
|
||||
|
||||
code := c.Request.FormValue("code")
|
||||
|
||||
info := GetSNSInfo(config,code,GG_TYPE)
|
||||
c.Writer.Write([]byte(fmt.Sprintf("Usern emailis %s and email is %s<br>", info.Name, info.Email)))
|
||||
}
|
||||
func main() {
|
||||
r := gin.Default()
|
||||
|
||||
r.GET("/", Home)
|
||||
r.GET("/SNSCallback", SNSCallback)
|
||||
|
||||
r.Run()
|
||||
}
|
61
oauth.go
Normal file
61
oauth.go
Normal file
|
@ -0,0 +1,61 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
"encoding/json"
|
||||
"golang.org/x/oauth2"
|
||||
"golang.org/x/net/context"
|
||||
"log"
|
||||
"io/ioutil"
|
||||
)
|
||||
|
||||
type UserSNSInfo struct {
|
||||
//Id string `json:"id"`
|
||||
Name string `json:"name"`
|
||||
Email string `json:"email"`
|
||||
AccessToken string
|
||||
}
|
||||
|
||||
func GetAccessToken(config *oauth2.Config, code string) *oauth2.Token {
|
||||
|
||||
token, err := config.Exchange(context.Background(), code)
|
||||
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
fmt.Println(token.AccessToken,"Expire :", token.Expiry, "type: ", token.TokenType)
|
||||
|
||||
return token
|
||||
}
|
||||
|
||||
func GetSNSInfo(config *oauth2.Config, code string, snsType SNSType) UserSNSInfo {
|
||||
|
||||
var response *http.Response
|
||||
var err error
|
||||
|
||||
accessToken := GetAccessToken(config, code)
|
||||
if snsType == FB_TYPE {
|
||||
response, err = http.Get("https://graph.facebook.com/me?fields=id,name,email&access_token=" + accessToken.AccessToken)
|
||||
}else if snsType == GG_TYPE {
|
||||
response, err = http.Get("https://www.googleapis.com/oauth2/v3/userinfo?access_token=" + accessToken.AccessToken)
|
||||
}
|
||||
|
||||
if err != nil {
|
||||
fmt.Println(err.Error())
|
||||
}
|
||||
|
||||
data, err := ioutil.ReadAll(response.Body)
|
||||
|
||||
if err != nil {
|
||||
fmt.Println(err.Error())
|
||||
}
|
||||
userinfo:= UserSNSInfo{}
|
||||
json.Unmarshal([]byte(data), &userinfo)
|
||||
userinfo.AccessToken = accessToken.AccessToken
|
||||
|
||||
fmt.Printf("Username %s and email is %s<br>", userinfo.Name, userinfo.Email)
|
||||
|
||||
return userinfo
|
||||
}
|
35
oauth_test.go
Normal file
35
oauth_test.go
Normal file
|
@ -0,0 +1,35 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"testing"
|
||||
"errors"
|
||||
"golang.org/x/oauth2"
|
||||
"golang.org/x/net/context"
|
||||
)
|
||||
|
||||
type appOAuthTransfer1 struct {
|
||||
rt func(req *http.Request) (resp *http.Response, err error)
|
||||
}
|
||||
|
||||
func (t *appOAuthTransfer1) RoundTrip(req *http.Request) (resp *http.Response, err error) {
|
||||
return t.rt(req)
|
||||
}
|
||||
|
||||
func TestExchangeRequest(t *testing.T) {
|
||||
tr := &appOAuthTransfer1{
|
||||
rt: func(r *http.Request) (w *http.Response, err error) {
|
||||
headerAuth := r.Header.Get("Authorization")
|
||||
if headerAuth != "" {
|
||||
t.Errorf("Unexpected authorization header, %v is found.", headerAuth)
|
||||
}
|
||||
return nil, errors.New("no response")
|
||||
},
|
||||
}
|
||||
|
||||
c := &http.Client{Transport: tr}
|
||||
|
||||
conf := getConfig(FB_TYPE)
|
||||
ctx := context.WithValue(context.Background(), oauth2.HTTPClient, c)
|
||||
conf.Exchange(ctx, "code")
|
||||
}
|
16
snsconst.go
Normal file
16
snsconst.go
Normal file
|
@ -0,0 +1,16 @@
|
|||
package main
|
||||
|
||||
const FB_CLIENT_ID = "808542632617259"
|
||||
const FB_CLIENT_SECRET = "8982cf312e1f31ba42f4ce847564aeff"
|
||||
|
||||
const GG_CLIENT_ID = "609607264245-qfq7odj24hi3minf4juhuje8445m2ro3.apps.googleusercontent.com"
|
||||
const GG_CLIENT_SECRET = "LUs0jpkLLhJtpKi5Zz5bDBo4"
|
||||
|
||||
const REDIRECT_URL = "http://localhost:8080/SNSCallback"
|
||||
|
||||
type SNSType int
|
||||
|
||||
const (
|
||||
FB_TYPE SNSType = 1
|
||||
GG_TYPE SNSType = 2
|
||||
)
|
Loading…
Reference in New Issue
Block a user