2019-10-09 16:41:55 +02:00
|
|
|
package sonia.scm.security;
|
|
|
|
|
|
|
|
|
|
import com.google.common.annotations.VisibleForTesting;
|
|
|
|
|
import com.google.inject.Inject;
|
|
|
|
|
import org.apache.shiro.authc.AuthenticationInfo;
|
|
|
|
|
import org.apache.shiro.authc.AuthenticationToken;
|
2019-10-10 13:40:40 +02:00
|
|
|
import org.apache.shiro.authc.credential.AllowAllCredentialsMatcher;
|
2019-10-09 16:41:55 +02:00
|
|
|
import org.apache.shiro.realm.AuthenticatingRealm;
|
2019-10-10 13:40:40 +02:00
|
|
|
import sonia.scm.SCMContext;
|
|
|
|
|
import sonia.scm.plugin.Extension;
|
2019-10-09 16:41:55 +02:00
|
|
|
|
2019-10-10 13:40:40 +02:00
|
|
|
import javax.inject.Singleton;
|
|
|
|
|
|
2019-10-17 09:26:36 +02:00
|
|
|
import static com.google.common.base.Preconditions.checkArgument;
|
|
|
|
|
|
2019-10-10 13:40:40 +02:00
|
|
|
@Singleton
|
|
|
|
|
@Extension
|
2019-10-09 16:41:55 +02:00
|
|
|
public class AnonymousRealm extends AuthenticatingRealm {
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* realm name
|
|
|
|
|
*/
|
|
|
|
|
@VisibleForTesting
|
|
|
|
|
static final String REALM = "AnonymousRealm";
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* dao realm helper
|
|
|
|
|
*/
|
|
|
|
|
private final DAORealmHelper helper;
|
|
|
|
|
|
|
|
|
|
@Inject
|
|
|
|
|
public AnonymousRealm(DAORealmHelperFactory helperFactory) {
|
|
|
|
|
this.helper = helperFactory.create(REALM);
|
|
|
|
|
|
|
|
|
|
setAuthenticationTokenClass(AnonymousToken.class);
|
2019-10-10 13:40:40 +02:00
|
|
|
setCredentialsMatcher(new AllowAllCredentialsMatcher());
|
2019-10-09 16:41:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
2019-10-17 09:26:36 +02:00
|
|
|
protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) {
|
|
|
|
|
checkArgument(authenticationToken instanceof AnonymousToken, "%s is required", AnonymousToken.class);
|
2019-10-10 13:40:40 +02:00
|
|
|
return helper.authenticationInfoBuilder(SCMContext.USER_ANONYMOUS).build();
|
2019-10-09 16:41:55 +02:00
|
|
|
}
|
|
|
|
|
}
|