Skip to content
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

Applying golint suggestions #11

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,10 @@ Your user type should meet the login.User interface:
Logout()

// Return the unique identifier of this user object
UniqueId() interface{}
UniqueID() interface{}

// Populate this user object with values
GetById(id interface{}) error
GetByID(id interface{}) error
}
```

Expand Down
8 changes: 4 additions & 4 deletions example/auth_example.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@ package main
import (
"database/sql"
"github.com/codegangsta/martini"
"github.com/coopernurse/gorp"
"github.com/martini-contrib/binding"
"github.com/martini-contrib/render"
"github.com/martini-contrib/sessionauth"
"github.com/martini-contrib/sessions"
"github.com/coopernurse/gorp"
_ "github.com/mattn/go-sqlite3"
"log"
"net/http"
Expand All @@ -32,7 +32,7 @@ func initDb() *gorp.DbMap {
}

dbmap := &gorp.DbMap{Db: db, Dialect: gorp.SqliteDialect{}}
dbmap.AddTableWithName(MyUserModel{}, "users").SetKeys(true, "Id")
dbmap.AddTableWithName(MyUserModel{}, "users").SetKeys(true, "ID")
err = dbmap.CreateTablesIfNotExists()
if err != nil {
log.Fatalln("Could not build tables", err)
Expand Down Expand Up @@ -60,7 +60,7 @@ func main() {
})
m.Use(sessions.Sessions("my_session", store))
m.Use(sessionauth.SessionUser(GenerateAnonymousUser))
sessionauth.RedirectUrl = "/new-login"
sessionauth.RedirectURL = "/new-login"
sessionauth.RedirectParam = "new-next"

m.Get("/", func(r render.Render) {
Expand All @@ -77,7 +77,7 @@ func main() {
user := MyUserModel{}
err := dbmap.SelectOne(&user, "SELECT * FROM users WHERE username = $1 and password = $2", postedUser.Username, postedUser.Password)
if err != nil {
r.Redirect(sessionauth.RedirectUrl)
r.Redirect(sessionauth.RedirectURL)
return
} else {
err := sessionauth.AuthenticateSession(session, &user)
Expand Down
10 changes: 5 additions & 5 deletions example/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (

// MyUserModel can be any struct that represents a user in my system
type MyUserModel struct {
Id int64 `form:"id" db:"id"`
ID int64 `form:"id" db:"id"`
Username string `form:"name" db:"username"`
Password string `form:"password" db:"password"`
authenticated bool `form:"-" db:"-"`
Expand Down Expand Up @@ -39,13 +39,13 @@ func (u *MyUserModel) IsAuthenticated() bool {
return u.authenticated
}

func (u *MyUserModel) UniqueId() interface{} {
return u.Id
func (u *MyUserModel) UniqueID() interface{} {
return u.ID
}

// GetById will populate a user object from a database model with
// GetByID will populate a user object from a database model with
// a matching id.
func (u *MyUserModel) GetById(id interface{}) error {
func (u *MyUserModel) GetByID(id interface{}) error {
err := dbmap.SelectOne(u, "SELECT * FROM users WHERE id = $1", id)
if err != nil {
return err
Expand Down
24 changes: 12 additions & 12 deletions sessionauth.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Package login is a middleware for Martini that provides a simple way to track user sessions
// Package sessionauth is a middleware for Martini that provides a simple way to track user sessions
// in on a website. Please see https://github.com/codegangsta/martini-contrib/blob/master/sessionauth/README.md
// for a more detailed description of the package.
package sessionauth
Expand All @@ -15,16 +15,16 @@ import (
// These are the default configuration values for this package. They
// can be set at anytime, probably during the initial setup of Martini.
var (
// RedirectUrl should be the relative URL for your login route
RedirectUrl string = "/login"
// RedirectURL should be the relative URL for your login route
RedirectURL = "/login"

// RedirectParam is the query string parameter that will be set
// with the page the user was trying to visit before they were
// intercepted.
RedirectParam string = "next"
RedirectParam = "next"

// SessionKey is the key containing the unique ID in your session
SessionKey string = "AUTHUNIQUEID"
SessionKey = "AUTHUNIQUEID"
)

// User defines all the functions necessary to work with the user's authentication.
Expand All @@ -41,10 +41,10 @@ type User interface {
Logout()

// Return the unique identifier of this user object
UniqueId() interface{}
UniqueID() interface{}

// Populate this user object with values
GetById(id interface{}) error
GetByID(id interface{}) error
}

// SessionUser will try to read a unique user ID out of the session. Then it tries
Expand All @@ -55,11 +55,11 @@ type User interface {
// user type.
func SessionUser(newUser func() User) martini.Handler {
return func(s sessions.Session, c martini.Context, l *log.Logger) {
userId := s.Get(SessionKey)
userID := s.Get(SessionKey)
user := newUser()

if userId != nil {
err := user.GetById(userId)
if userID != nil {
err := user.GetByID(userID)
if err != nil {
l.Printf("Login Error: %v\n", err)
} else {
Expand Down Expand Up @@ -91,14 +91,14 @@ func Logout(s sessions.Session, user User) {
// set to the attempted URL.
func LoginRequired(r render.Render, user User, req *http.Request) {
if user.IsAuthenticated() == false {
path := fmt.Sprintf("%s?%s=%s", RedirectUrl, RedirectParam, req.URL.Path)
path := fmt.Sprintf("%s?%s=%s", RedirectURL, RedirectParam, req.URL.Path)
r.Redirect(path, 302)
}
}

// UpdateUser updates the User object stored in the session. This is useful incase a change
// is made to the user model that needs to persist across requests.
func UpdateUser(s sessions.Session, user User) error {
s.Set(SessionKey, user.UniqueId())
s.Set(SessionKey, user.UniqueID())
return nil
}
10 changes: 5 additions & 5 deletions sessionauth_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
)

type TestUser struct {
Id int `json:"id"`
ID int `json:"id"`
Name string `json:"name"`
Age int `json:"age"`
authenticated bool `json:"-"`
Expand All @@ -28,12 +28,12 @@ func (u *TestUser) Logout() {
u.authenticated = false
}

func (u *TestUser) UniqueId() interface{} {
return u.Id
func (u *TestUser) UniqueID() interface{} {
return u.ID
}

func (u *TestUser) GetById(id interface{}) error {
u.Id = id.(int)
func (u *TestUser) GetByID(id interface{}) error {
u.ID = id.(int)
u.Name = "My Test User"
u.Age = 42

Expand Down