Skip to content

Commit

Permalink
added IP to session
Browse files Browse the repository at this point in the history
  • Loading branch information
sagostin committed Sep 20, 2024
1 parent 31f1368 commit fe0a114
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 9 deletions.
6 changes: 4 additions & 2 deletions internal/auth/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ type Login struct {
}

// Login returns error on fail, nil on success
func (r *Login) Login(db *mongo.Database) (string, error) {
func (r *Login) Login(ip string, db *mongo.Database) (string, error) {
if r.Email == "" {
ee := internal.ErrorFormat{Package: "internal.auth", Level: log.ErrorLevel, Function: "auth.Login", Message: "invalid email address"}
ee.Print()
Expand All @@ -45,6 +45,7 @@ func (r *Login) Login(db *mongo.Database) (string, error) {
session := Session{
ID: user.ID,
IsAgent: false,
IP: ip,
}

err = session.Create(db)
Expand Down Expand Up @@ -202,7 +203,7 @@ type AgentLogin struct {
}

// AgentLogin returns error on fail, nil on success
func (r *AgentLogin) AgentLogin(db *mongo.Database) (string, error) {
func (r *AgentLogin) AgentLogin(ip string, db *mongo.Database) (string, error) {
ee := internal.ErrorFormat{Package: "internal.auth", Level: log.ErrorLevel, Function: "auth.AgentLogin"}

if r.PIN == "" {
Expand Down Expand Up @@ -241,6 +242,7 @@ func (r *AgentLogin) AgentLogin(db *mongo.Database) (string, error) {
session := Session{
ID: u.ID,
IsAgent: true,
IP: ip,
}

err = session.Create(db)
Expand Down
11 changes: 6 additions & 5 deletions internal/auth/session.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,12 @@ import (
)

type Session struct {
ID primitive.ObjectID `json:"item_id"bson:"item_id"`
IsAgent bool `json:"is_agent"bson:"is_agent"`
SessionID primitive.ObjectID `json:"session_id"bson:"_id"`
Expiry time.Time `json:"expiry"bson:"expiry"`
WSConn string `json:"ws_conn"bson:"ws_conn"`
ID primitive.ObjectID `json:"item_id" bson:"item_id"`
IsAgent bool `json:"is_agent" bson:"is_agent"`
SessionID primitive.ObjectID `json:"session_id" bson:"_id"`
Expiry time.Time `json:"expiry" bson:"expiry"`
WSConn string `json:"ws_conn" bson:"ws_conn"`
IP string `json:"ip,omitempty" bson:"ip"`
}

// Create a session from user id, and include expiry, return error if fails
Expand Down
2 changes: 1 addition & 1 deletion web/agent_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ func addRouteAgentAPI(r *Router) []*Route {
return nil
}

t, err := l.AgentLogin(r.DB)
t, err := l.AgentLogin(ctx.RemoteAddr(), r.DB)
if err != nil {
ctx.StatusCode(http.StatusUnauthorized)
return nil
Expand Down
2 changes: 1 addition & 1 deletion web/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ func addRouteAuth(r *Router) []*Route {
return nil
}

t, err := l.Login(r.DB)
t, err := l.Login(ctx.RemoteAddr(), r.DB)
if err != nil {
ctx.StatusCode(http.StatusUnauthorized)
return nil
Expand Down

0 comments on commit fe0a114

Please sign in to comment.