/
google.go
66 lines (60 loc) · 1.92 KB
/
google.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
package oauthService
import (
"encoding/json"
"io/ioutil"
"net/http"
"github.com/dorajistyle/goyangi/util/log"
"github.com/dorajistyle/goyangi/util/modelHelper"
"github.com/dorajistyle/goyangi/util/oauth2"
"github.com/dorajistyle/goyangi/util/oauth2/google"
"github.com/gin-gonic/gin"
"github.com/gin-gonic/gin/binding"
)
// GoogleURL return google auth url.
func GoogleURL() (string, int) {
return oauth2.OauthURL(google.Config), http.StatusOK
}
// SetGoogleUser set google user.
func SetGoogleUser(response *http.Response) (*GoogleUser, error) {
googleUser := &GoogleUser{}
log.Debugf("\nresponse: %v\n", response)
defer response.Body.Close()
body, err := ioutil.ReadAll(response.Body)
if err != nil {
return googleUser, err
}
unmarshalErr := json.Unmarshal(body, &googleUser)
if unmarshalErr != nil {
return googleUser, unmarshalErr
}
return googleUser, err
}
// OauthGoogle link connection and user.
func OauthGoogle(c *gin.Context) (int, error) {
log.Debugf("c.Request.URL : %s", c.Request.URL)
var authResponse oauth2.AuthResponse
var oauthUser OauthUser
bindErr := c.MustBindWith(&authResponse, binding.Form)
log.Debugf("bind error : %s\n", bindErr)
log.Debugf("oauthRedirect form: %v", authResponse)
response, token, err := oauth2.OauthRequest(google.RequestURL, google.Config, authResponse)
if err != nil {
return http.StatusInternalServerError, err
}
googleUser, err := SetGoogleUser(response)
if err != nil {
return http.StatusInternalServerError, err
}
modelHelper.AssignValue(&oauthUser, googleUser)
oauthUser.Email = googleUser.Emails[0].Value
oauthUser.ImageUrl = googleUser.Image.URL
status, err := LoginOrCreateOauthUser(c, &oauthUser, google.ProviderId, token)
if err != nil {
return status, err
}
return http.StatusSeeOther, nil
}
// RevokeGoogle revokes google oauth connection.
func RevokeGoogle(c *gin.Context) (map[string]bool, int, error) {
return RevokeOauth(c, google.ProviderId)
}