-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoauth_handler.go
127 lines (108 loc) · 3.62 KB
/
oauth_handler.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
package fundrive
import (
"github.com/gofiber/fiber/v2"
"golang.org/x/oauth2"
"gorm.io/gorm"
"log"
"net/http"
"net/url"
)
type OAuthHandler struct {
oauth2Config *oauth2.Config
db *gorm.DB
tokenEncryptor *TokenEncryption
}
func NewOAuthHandler(
oauth2Config *oauth2.Config,
db *gorm.DB,
tokenEncryptor *TokenEncryption,
) OAuthHandler {
return OAuthHandler{
oauth2Config: oauth2Config,
db: db,
tokenEncryptor: tokenEncryptor,
}
}
func (handler *OAuthHandler) Route(app *fiber.App) {
app.Get("/auth/google/authorize", handler.AuthorizeHandler)
app.Get("/google-drive", handler.AuthorizeCallbackHandler)
}
func (handler *OAuthHandler) Run(app *fiber.App, port string) {
log.Println("Listening on port 3000")
err := app.Listen(":" + port)
if err != nil {
log.Fatal(err)
}
}
type AuthorizeResponse struct {
URL string `json:"url"`
}
func (handler *OAuthHandler) AuthorizeHandler(c *fiber.Ctx) error {
redirectURL := c.Query("redirect_url", "http://localhost:3000/google-drive")
queryParams := url.Values{
"redirect_url": {redirectURL},
}
// https://medium.com/starthinker/google-oauth-2-0-access-token-and-refresh-token-explained-cccf2fc0a6d9
authCodeOpt := []oauth2.AuthCodeOption{
oauth2.AccessTypeOffline, // obtain the refresh token
oauth2.ApprovalForce, // forces the users to view the consent dialog
}
loginUrl := handler.oauth2Config.AuthCodeURL(queryParams.Encode(), authCodeOpt...)
return c.Redirect(loginUrl, http.StatusFound)
}
func (handler *OAuthHandler) AuthorizeCallbackHandler(c *fiber.Ctx) error {
code := c.Query("code")
token, err := handler.oauth2Config.Exchange(c.Context(), code)
if err != nil {
return c.Status(500).JSON(fiber.Map{
"code": http.StatusInternalServerError,
"success": false,
"message": http.StatusText(http.StatusInternalServerError),
"details": err.Error(),
})
}
oAuthConfig := OAuthConfig{
DB: handler.db,
OAuth2Config: handler.oauth2Config,
TokenEncryptor: handler.tokenEncryptor,
}
oauthService, err := NewOAuthService(&oAuthConfig)
if err != nil {
return c.Status(500).JSON(fiber.Map{
"code": http.StatusInternalServerError,
"success": false,
"message": http.StatusText(http.StatusInternalServerError),
"details": err.Error(),
})
}
userInfo, err := oauthService.GetGoogleUserInfo(c.Context(), &GetUserInfoRequest{Token: token})
if err != nil {
return c.Status(500).JSON(fiber.Map{
"code": http.StatusInternalServerError,
"success": false,
"message": http.StatusText(http.StatusInternalServerError),
"details": err.Error(),
})
}
userInfo.ID = "user-dev" // dummy user, simulate multiple email with same ID
saveTokenReq := SaveTokenRequest{
UserID: userInfo.ID,
Email: userInfo.Email,
Token: token,
}
err = oauthService.SaveToken(c.Context(), &saveTokenReq)
if err != nil {
return c.Status(500).JSON(fiber.Map{
"code": http.StatusInternalServerError,
"success": false,
"message": http.StatusText(http.StatusInternalServerError),
"details": err.Error(),
})
}
return c.Status(200).JSON(fiber.Map{
"code": http.StatusOK,
"success": true,
"message": http.StatusText(http.StatusOK),
"details": userInfo,
})
}