Skip to content

Commit

Permalink
Merge pull request #61 from shion1305/change-auth-redirect-token-2402
Browse files Browse the repository at this point in the history
🛂 認証情報付与形式の変更
  • Loading branch information
Shion1305 authored Feb 22, 2024
2 parents 00accdb + 31cb957 commit cdf7276
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 26 deletions.
4 changes: 3 additions & 1 deletion svc/pkg/handler/agent/org.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package agent

import (
"fmt"
"github.com/gin-gonic/gin"
"log"
"time"
Expand Down Expand Up @@ -53,7 +54,8 @@ func (o Org) CreateHandler() gin.HandlerFunc {
opt, err := o.createOrgUC.Do(ipt)
if err != nil {
log.Printf("failed to create org in CreateOrgHandler: %v", err)
c.AbortWithStatusJSON(500, gin.H{"error": "failed to create org"})
c.AbortWithStatusJSON(500,
gin.H{"error": fmt.Sprintf("failed to create org: %v", err)})
return
}
resp := agent.CreateOrgResponse{
Expand Down
42 changes: 17 additions & 25 deletions svc/pkg/handler/line/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,48 +56,40 @@ func (a LineAuth) VerificationHandler() gin.HandlerFunc {
Ctx: c,
}
authOut, err := a.authUC.Do(authInput)

if err != nil {
_, _ = c.Writer.WriteString(err.Error())
log.Printf("error: %v", err)
c.AbortWithStatus(500)
return
}
err = a.setCookie(c, authOut.UserInfo.ID.ExportID())

var redirectDest string
if a.serverConf.OnProduction {
redirectDest = "/token"
} else {
redirectDest = fmt.Sprintf(
"%s%s%s/token", a.serverConf.Frontend.Protocol, a.serverConf.Frontend.Domain, a.serverConf.Frontend.Port,
)
}
redirectDest, err = a.attachToken(authOut.UserInfo.ID.ExportID(), redirectDest)
if err != nil {
log.Println(c, "failed to set cookie: %v", err)
log.Printf("failed to attach token: %v", err)
c.AbortWithStatus(500)
return
}
if a.serverConf.OnProduction {
if !authOut.UserInfo.Detail.MeetsBasicRequirement() {
c.Redirect(302, "/welcome")
return
}
c.Redirect(302, "/")
} else {
front := a.serverConf.Frontend
if !authOut.UserInfo.Detail.MeetsBasicRequirement() {
c.Redirect(302,
fmt.Sprintf("%s%s%s/welcome", front.Protocol, front.Domain, front.Port))
return
}
c.Redirect(302,
fmt.Sprintf("%s%s%s/", front.Protocol, front.Domain, front.Port))
}
c.Redirect(302, redirectDest)
}
}

func (a LineAuth) setCookie(c *gin.Context, id string) error {
func (a LineAuth) attachToken(id string, dest string) (string, error) {
// maxAge is set to 1 day
claim := jwt.CreateClaims(id, 24*time.Hour, a.serverConf.Backend.Domain)
token, err := jwt.IssueJWT(claim, config.JWT.JWTSecret)
if err != nil {
return err
return "", err
}
// maxAge is set to 1 day
c.SetCookie("Authorization", token, 3600*24,
"/", a.serverConf.Frontend.Domain, a.secureCookie, false)
return nil
dest = fmt.Sprintf("%s?token=%s", dest, token)
return dest, nil
}

func (a LineAuth) StateIssuer() gin.HandlerFunc {
Expand Down

0 comments on commit cdf7276

Please sign in to comment.