-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #9 from gdsc-ncku/Feat/line-login
line login
- Loading branch information
Showing
10 changed files
with
171 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,7 +3,7 @@ APP_DB_HOST=localhost | |
APP_DB_PORT=5432 | ||
APP_DB_USERNAME=test_user | ||
APP_DB_PASSWORD=test_password | ||
APP_DB_DATABASE=testdb | ||
APP_DB_DATABASE=postgres | ||
|
||
APP_REDIS_HOST=localhost | ||
APP_REDIS_PORT=6379 | ||
|
@@ -16,6 +16,9 @@ APP_JWT_REFRESH_SECRET=secret | |
APP_JWT_ACCESS_EXPIRY=3600 | ||
APP_JWT_REFRESH_EXPIRY=86400 | ||
|
||
APP_LINE_CHANNEL_ID=secret | ||
APP_LINE_CHANNEL_SECRET=secret | ||
|
||
APP_DOMAIN=:80 | ||
|
||
APP_PGADMIN_EMAIL=[email protected] | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package bootstrap | ||
|
||
import social "github.com/kkdai/line-login-sdk-go" | ||
|
||
type LineEnv struct { | ||
ChannelID string `env:"CHANNEL_ID"` | ||
ChannelSecret string `env:"CHANNEL_SECRET"` | ||
} | ||
|
||
func NewLineSocialClient(env *Env) *social.Client { | ||
client, _ := social.New(env.Line.ChannelID, env.Line.ChannelSecret) | ||
return client | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,117 @@ | ||
package controller | ||
|
||
import ( | ||
"bikefest/pkg/bootstrap" | ||
"bikefest/pkg/model" | ||
"fmt" | ||
"github.com/gin-gonic/gin" | ||
social "github.com/kkdai/line-login-sdk-go" | ||
"log" | ||
"net/http" | ||
"strconv" | ||
) | ||
|
||
func NewOAuthController(lineSocialClient *social.Client, env *bootstrap.Env, userSvc model.UserService) *OAuthController { | ||
return &OAuthController{ | ||
lineSocialClient: lineSocialClient, | ||
userSvc: userSvc, | ||
env: env, | ||
} | ||
} | ||
|
||
type OAuthController struct { | ||
lineSocialClient *social.Client | ||
userSvc model.UserService | ||
env *bootstrap.Env | ||
} | ||
|
||
// http://localhost:8000/line-login/auth | ||
func (ctrl *OAuthController) LineLogin(c *gin.Context) { | ||
//TODO: place `serverURL` into env | ||
serverURL := "http://localhost:8000" | ||
scope := "profile openid" //profile | openid | email | ||
state := social.GenerateNonce() | ||
nonce := social.GenerateNonce() | ||
redirectURL := fmt.Sprintf("%s/line-login/callback", serverURL) | ||
targetURL := ctrl.lineSocialClient.GetWebLoinURL(redirectURL, state, scope, social.AuthRequestOptions{Nonce: nonce, Prompt: "consent"}) | ||
c.Redirect(http.StatusMovedPermanently, targetURL) | ||
} | ||
|
||
func (ctrl *OAuthController) LineLoginCallback(c *gin.Context) { | ||
//TODO: place `serverURL` and `frontendURL` into env | ||
serverURL := "http://localhost:8000" | ||
frontendURL := "http://localhost:3000" | ||
code := c.Query("code") | ||
_ = c.Query("state") | ||
token, err := ctrl.lineSocialClient.GetAccessToken(fmt.Sprintf("%s/line-login/callback", serverURL), code).Do() | ||
if err != nil { | ||
log.Println("RequestLoginToken err:", err) | ||
return | ||
} | ||
log.Println("access_token:", token.AccessToken, " refresh_token:", token.RefreshToken) | ||
|
||
var payload *social.Payload | ||
//if len(token.IDToken) == 0 { | ||
// // User don't request openID, use access token to get user profile | ||
// log.Println(" token:", token, " AccessToken:", token.AccessToken) | ||
// res, err := ctrl.lineSocialClient.GetUserProfile(token.AccessToken).Do() | ||
// if err != nil { | ||
// log.Println("GetUserProfile err:", err) | ||
// return | ||
// } | ||
// payload = &social.Payload{ | ||
// Name: res.DisplayName, | ||
// Picture: res.PictureURL, | ||
// } | ||
//} else { | ||
//Decode token.IDToken to payload | ||
payload, err = token.DecodePayload(ctrl.env.Line.ChannelID) | ||
if err != nil { | ||
log.Println("DecodeIDToken err:", err) | ||
return | ||
} | ||
//} | ||
log.Printf("payload: %#v", payload) | ||
|
||
//c.JSON(http.StatusOK, gin.H{ | ||
// "status": "Success", | ||
// "data": payload, | ||
//}) | ||
|
||
user := &model.User{ | ||
ID: payload.Sub, | ||
Name: payload.Name, | ||
} | ||
|
||
err = ctrl.userSvc.CreateFakeUser(c, user) | ||
|
||
if err != nil { | ||
log.Printf("user with id %s already exists", user.ID) | ||
} | ||
|
||
accessToken, err := ctrl.userSvc.CreateAccessToken(c, user, ctrl.env.JWT.AccessTokenSecret, ctrl.env.JWT.AccessTokenExpiry) | ||
if err != nil { | ||
log.Printf("failed to create access token: %v", err) | ||
c.JSON(http.StatusInternalServerError, gin.H{ | ||
"status": "Failed", | ||
"message": "failed to create access token", | ||
}) | ||
return | ||
} | ||
|
||
refreshToken, err := ctrl.userSvc.CreateRefreshToken(c, user, ctrl.env.JWT.RefreshTokenSecret, ctrl.env.JWT.RefreshTokenExpiry) | ||
if err != nil { | ||
log.Printf("failed to create refresh token: %v", err) | ||
c.JSON(http.StatusInternalServerError, gin.H{ | ||
"status": "Failed", | ||
"message": "failed to create refresh token", | ||
}) | ||
return | ||
} | ||
|
||
// set to cookie | ||
c.SetCookie("access_token", strconv.FormatInt(ctrl.env.JWT.AccessTokenExpiry, 10), 3600, "/", "", false, true) | ||
c.SetCookie("refresh_token", strconv.FormatInt(ctrl.env.JWT.AccessTokenExpiry, 10), 3600, "/", "", false, true) | ||
// redirect to frontend | ||
c.Redirect(http.StatusMovedPermanently, fmt.Sprintf("%s/oauth?access_token=%s&refresh_token=%s", frontendURL, accessToken, refreshToken)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package router | ||
|
||
import ( | ||
"bikefest/pkg/bootstrap" | ||
"bikefest/pkg/controller" | ||
) | ||
|
||
func RegisterOAuthRouter(app *bootstrap.Application, controller *controller.OAuthController) { | ||
lineRouter := app.Engine.Group("/line-login") | ||
lineRouter.GET("/auth", controller.LineLogin) | ||
lineRouter.GET("/callback", controller.LineLoginCallback) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters