-
Notifications
You must be signed in to change notification settings - Fork 88
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weβll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: [#547] Auth supports session driver #820
Open
KlassnayaAfrodita
wants to merge
4
commits into
goravel:master
Choose a base branch
from
KlassnayaAfrodita:patch-4
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 |
---|---|---|
@@ -0,0 +1,153 @@ | ||
package auth | ||
|
||
import ( | ||
"fmt" | ||
"time" | ||
|
||
"github.com/spf13/cast" | ||
"gorm.io/gorm/clause" | ||
|
||
contractsauth "github.com/goravel/framework/contracts/auth" | ||
"github.com/goravel/framework/contracts/cache" | ||
"github.com/goravel/framework/contracts/config" | ||
"github.com/goravel/framework/contracts/database/orm" | ||
"github.com/goravel/framework/contracts/http" | ||
"github.com/goravel/framework/errors" | ||
"github.com/goravel/framework/support/database" | ||
) | ||
|
||
const sessionCtxKey = "GoravelSessionAuth" | ||
|
||
type Session struct { | ||
SessionID string | ||
} | ||
|
||
type Sessions map[string]*Session | ||
|
||
type SessionAuth struct { | ||
cache cache.Cache | ||
config config.Config | ||
ctx http.Context | ||
session string | ||
orm orm.Orm | ||
} | ||
|
||
func NewSessionAuth(session string, cache cache.Cache, config config.Config, ctx http.Context, orm orm.Orm) *SessionAuth { | ||
return &SessionAuth{ | ||
cache: cache, | ||
config: config, | ||
ctx: ctx, | ||
session: session, | ||
orm: orm, | ||
} | ||
} | ||
|
||
func (a *SessionAuth) Session(name string) contractsauth.Auth { | ||
return NewAuth(name, a.cache, a.config, a.ctx, a.orm) | ||
} | ||
|
||
func (a *SessionAuth) SessionUser(user any) error { | ||
auth, ok := a.ctx.Value(sessionCtxKey).(Sessions) | ||
if !ok || auth[a.session] == nil { | ||
return errors.AuthParseTokenFirst | ||
} | ||
if auth[a.session].SessionID == "" { | ||
return errors.AuthInvalidKey | ||
} | ||
|
||
if err := a.orm.Query().FindOrFail(user, clause.Eq{Column: clause.PrimaryColumn, Value: auth[a.session].SessionID}); err != nil { | ||
return err | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func (a *SessionAuth) SessionID() (string, error) { | ||
auth, ok := a.ctx.Value(sessionCtxKey).(Sessions) | ||
if !ok || auth[a.session] == nil { | ||
return "", errors.AuthParseTokenFirst | ||
} | ||
if auth[a.session].SessionID == "" { | ||
return "", errors.AuthInvalidKey | ||
} | ||
|
||
return auth[a.session].SessionID, nil | ||
} | ||
|
||
func (a *SessionAuth) SessionLogin(user any) (string, error) { | ||
id := database.GetID(user) | ||
if id == nil { | ||
return "", errors.AuthNoPrimaryKeyField | ||
} | ||
|
||
sessionID := cast.ToString(id) | ||
if sessionID == "" { | ||
return "", errors.AuthInvalidKey | ||
} | ||
|
||
if err := a.cache.Put(getSessionCacheKey(sessionID), true, time.Duration(a.getSessionTtl())*time.Minute); err != nil { | ||
return "", err | ||
} | ||
|
||
a.makeSessionAuthContext(sessionID) | ||
|
||
return sessionID, nil | ||
} | ||
|
||
func (a *SessionAuth) SessionLogout() error { | ||
auth, ok := a.ctx.Value(sessionCtxKey).(Sessions) | ||
if !ok || auth[a.session] == nil || auth[a.session].SessionID == "" { | ||
return nil | ||
} | ||
|
||
if err := a.cache.Put(getSessionCacheKey(auth[a.session].SessionID), true, time.Duration(a.getSessionTtl())*time.Minute); err != nil { | ||
return err | ||
} | ||
|
||
delete(auth, a.session) | ||
a.ctx.WithValue(sessionCtxKey, auth) | ||
|
||
return nil | ||
} | ||
|
||
func (a *SessionAuth) SessionRefresh() (string, error) { | ||
auth, ok := a.ctx.Value(sessionCtxKey).(Sessions) | ||
if !ok || auth[a.session] == nil { | ||
return "", errors.AuthParseTokenFirst | ||
} | ||
|
||
if !a.cache.GetBool(getSessionCacheKey(auth[a.session].SessionID), false) { | ||
return "", errors.AuthTokenExpired | ||
} | ||
|
||
return auth[a.session].SessionID, nil | ||
} | ||
|
||
func (a *SessionAuth) makeSessionAuthContext(sessionID string) { | ||
sessions, ok := a.ctx.Value(sessionCtxKey).(Sessions) | ||
if !ok { | ||
sessions = make(Sessions) | ||
} | ||
sessions[a.session] = &Session{SessionID: sessionID} | ||
a.ctx.WithValue(sessionCtxKey, sessions) | ||
} | ||
|
||
func (a *SessionAuth) getSessionTtl() int { | ||
var ttl int | ||
SessionTtl := a.config.Get(fmt.Sprintf("auth.Sessions.%s.ttl", a.session)) | ||
if SessionTtl == nil { | ||
ttl = a.config.GetInt("session.ttl") | ||
} else { | ||
ttl = cast.ToInt(SessionTtl) | ||
} | ||
|
||
if ttl == 0 { | ||
ttl = 60 * 24 * 30 | ||
} | ||
|
||
return ttl | ||
} | ||
|
||
func getSessionCacheKey(sessionID string) string { | ||
return "session:" + sessionID | ||
} |
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,64 @@ | ||
package auth | ||
|
||
import ( | ||
"errors" | ||
"github.com/goravel/framework/contracts/cache" | ||
"github.com/goravel/framework/contracts/config" | ||
"github.com/goravel/framework/contracts/database/orm" | ||
"github.com/goravel/framework/contracts/http" | ||
) | ||
|
||
type JWTDriver struct { | ||
auth *Auth | ||
} | ||
|
||
func NewJWTDriver(config config.Config, cache cache.Cache, ctx http.Context, orm orm.Orm) *JWTDriver { | ||
return &JWTDriver{ | ||
auth: NewAuth("jwt", cache, config, ctx, orm), | ||
} | ||
} | ||
|
||
func (d *JWTDriver) Login(userID string, data map[string]interface{}) (string, error) { | ||
if userID == "" { | ||
return "", errors.New("user ID is required") | ||
} | ||
|
||
token, err := d.auth.LoginUsingID(userID) | ||
if err != nil { | ||
return "", err | ||
} | ||
|
||
return token, nil | ||
} | ||
|
||
func (d *JWTDriver) Logout(sessionID string) error { | ||
if sessionID == "" { | ||
return errors.New("session ID is required") | ||
} | ||
|
||
d.auth.ctx.WithValue(ctxKey, Guards{ | ||
"jwt": &Guard{ | ||
Token: sessionID, | ||
}, | ||
}) | ||
|
||
err := d.auth.Logout() | ||
if err != nil { | ||
return err | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func (d *JWTDriver) Authenticate(sessionID string) error { | ||
if sessionID == "" { | ||
return errors.New("session ID is required") | ||
} | ||
|
||
_, err := d.auth.Parse(sessionID) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
return nil | ||
} |
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,7 @@ | ||
package auth | ||
|
||
type Driver interface { | ||
Login(userID string, data map[string]interface{}) (string, error) // User login | ||
Logout(sessionID string) error // User logout | ||
Authenticate(sessionID string) error // Check authentication | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
File name: auth_session.go -> session.go