-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #138 from Paul-Blanchaert/enable-pac4j-basicauth
Enable basic auth from configured user/password
- Loading branch information
Showing
4 changed files
with
57 additions
and
10 deletions.
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,30 @@ | ||
package modules | ||
|
||
import org.pac4j.core.context.WebContext | ||
import org.pac4j.core.context.session.SessionStore | ||
import org.pac4j.core.credentials.{Credentials, UsernamePasswordCredentials} | ||
import org.pac4j.core.credentials.authenticator.Authenticator | ||
import org.pac4j.core.exception.CredentialsException | ||
import org.pac4j.core.profile.CommonProfile | ||
import org.pac4j.core.util.{CommonHelper, Pac4jConstants} | ||
|
||
|
||
case class ConfiguredBasicAuthAuthenticator(validUserId: String, validPassword: String) extends Authenticator { | ||
|
||
override def validate(credentials: Credentials, context: WebContext, sessionStore: SessionStore): Unit = { | ||
if (credentials == null) throw new CredentialsException("No credential") | ||
val userCredentials = credentials.asInstanceOf[UsernamePasswordCredentials] | ||
val username = userCredentials.getUsername() | ||
val password = userCredentials.getPassword() | ||
if (CommonHelper.isBlank(username)) throw new CredentialsException("Username cannot be blank") | ||
if (CommonHelper.isBlank(password)) throw new CredentialsException("Password cannot be blank") | ||
if (CommonHelper.areNotEquals(username, validUserId)) throw new CredentialsException("Username : '" + username + "' does not match valid user") | ||
if (CommonHelper.areNotEquals(password, validPassword)) throw new CredentialsException("Password does not match valid password") | ||
val profile = new CommonProfile | ||
profile.setId(username) | ||
profile.addAttribute(Pac4jConstants.USERNAME, username) | ||
userCredentials.setUserProfile(profile) | ||
} | ||
|
||
} | ||
|
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
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
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