Skip to content

Commit

Permalink
Merge pull request #71 from ochanoco/hotfix/attach-userid-when-auth-u…
Browse files Browse the repository at this point in the history
…nforced

fix: attach x-torima-userid when auth is not forced
  • Loading branch information
akakou authored Jul 3, 2024
2 parents 1c22a90 + b10e242 commit 8e4e941
Showing 1 changed file with 8 additions and 15 deletions.
23 changes: 8 additions & 15 deletions extension/directors/auth.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package directors

import (
"fmt"

"github.com/ochanoco/ninsho"
gin_ninsho "github.com/ochanoco/ninsho/extension/gin"
"github.com/ochanoco/torima/core"
Expand All @@ -26,24 +24,19 @@ func SkipAuthDirector(c *core.TorimaDirectorPackageContext) (core.TorimaPackageS
}

func AuthDirector(c *core.TorimaDirectorPackageContext) (core.TorimaPackageStatus, error) {
if c.PackageStatus == core.NoAuthNeeded {
return core.NoAuthNeeded, nil
}

user, err := gin_ninsho.LoadUser[ninsho.LINE_USER](c.GinContext)

// just to be sure
c.Target.Header.Del("X-Torima-UserID")

if err != nil {
err = utils.MakeError(err, "failed to get user from session: ")
return core.ForceStop, err
}
if err != nil || user == nil {
if c.PackageStatus == core.NoAuthNeeded {
return core.NoAuthNeeded, nil
}

if user != nil {
c.Target.Header.Set("X-Torima-UserID", user.Sub)
return core.Authed, nil
err = utils.MakeError(err, "failed to authenticate: ")
return core.ForceStop, err
}

return core.ForceStop, utils.MakeError(fmt.Errorf(""), utils.UnauthorizedErrorTag)
c.Target.Header.Set("X-Torima-UserID", user.Sub)
return core.Authed, nil
}

0 comments on commit 8e4e941

Please sign in to comment.