gin icon indicating copy to clipboard operation
gin copied to clipboard

feat: basic auth without keeping a list of accounts

Open stefanbildl opened this issue 1 year ago • 0 comments

Hi, while using gin I often wanted to use basic authentication that does not require me to save all available accounts to a map (Currently it is handled that way).

I wanted to be able to pass a username and password validator that directly "plugs into" gin.

Example:

I have a function that checks if a user and password combination is right.

func isValidUser(username string, password string) (valid bool) {
    // e.g. LDAP authentication is done here 
    // if user and password match, return true
}

It does not include any basic authentication specific code. Realm and authentication headers are hidden from the user of the library. This function (of type UsernamePasswordValidator) can now be converted to a gin basic authentication middleware like so:

middleware := BasicAuthForRealmWithValidator(isValidUser, "")

I added tests and refactored the current code so that it integrates seamlessly with this new behavior.

I hope you like it.

Thank you for this great library. It is a joy to use.

stefanbildl avatar Apr 09 '23 11:04 stefanbildl