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

Add auth.BasicFunc #7

Merged
merged 6 commits into from
Jul 8, 2014
Merged

Add auth.BasicFunc #7

merged 6 commits into from
Jul 8, 2014

Conversation

attilaolah
Copy link
Contributor

This only includes the implementation and some tests. Give me a moment and I'll update the README.

@attilaolah attilaolah mentioned this pull request Apr 28, 2014
@attilaolah
Copy link
Contributor Author

Added documentation. Should be good to merge.

@codegangsta codegangsta merged commit b135034 into martini-contrib:master Jul 8, 2014
@attilaolah attilaolah deleted the auth-basicfunc branch July 8, 2014 15:17
@schickling
Copy link

I'm still not sure how to inject something into the middleware e.g. a DB object.

@schickling
Copy link

@attilaolah @codegangsta could someone provide an example please?

@attilaolah
Copy link
Contributor Author

@schickling just make it available to the closure you pass in to auth.BasicFunc, something like:

db := dbSetup(…)
m.Use(auth.BasicFunc(func(username, password string) bool {
    user := db.Get(username)
    return user != nil && auth.SecureCompare(password, user.Password)
})

@schickling
Copy link

Is there another way by using the dependency injection inside via the m.Use(...) params? I don't think its a good idea to have a global db variable.

@attilaolah
Copy link
Contributor Author

I don't think its a good idea to have a global db variable.

Of course it doesn't have to be global.

Currently auth.BasicFunc does not support Martini-style DI. What you can try instead, if you really don't like the closure, is to pass in a struct method instead:

type DBAuth struct {
    db Database
}

func (a *DBAuth) auth(username, password string) bool {
    user := a.db.get(username)
    return user != nil && auth.SecureCompare(user.Password, password)
}

func main() {
    // …
    m.Use(auth.BasicFunc((&DBAuth{
        db: dbSetup(),
    }).auth))
}

This has basically the same effect as using a closure, only it uses a struct instead.

@schickling
Copy link

Thanks for the solution! Are there any plans to add DI to this project?

@attilaolah
Copy link
Contributor Author

There were some discussions earlier, but I'm not sure anyone's too keen on implementing this. See #18.

I know the DI is cool but I still prefer good old closures or struct methods. But if you want to implement it, feel free to do so and send a PR.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants