My usecase was this: I had a set of small OSGi web whiteboard web applications running in apache karaf fronted by nginx and I wanted to have the same login and set of users across all web application, and I wanted to have the same forms based for nginx as well. A sort of "poor man's single signon".
This project was my solution.
This project contains a set of [[https://karaf.apache.org/manual/latest/#_feature_and_resolver][apache karaf features]] that fills two purposes
- Providing a forms based login mechanism for nginx (Note: the webapp provides only authentication. No authorization of individual URLs. All authenticated users get in)
- Providing a "poor man's single sign-on" for web applications running in the same apache karaf instance
Status of the project
file:https://travis-ci.org/steinarb/authservice.svg?branch=master file:https://coveralls.io/repos/steinarb/authservice/badge.svg file:https://sonarcloud.io/api/project_badges/measure?project=no.priv.bang.authservice%3Aauthservice&metric=alert_status#.svg file:https://maven-badges.herokuapp.com/maven-central/no.priv.bang.authservice/authservice/badge.svg file:https://www.javadoc.io/badge/no.priv.bang.authservice/authservice.svg
SonarCloud
file:https://sonarcloud.io/api/project_badges/measure?project=no.priv.bang.authservice%3Aauthservice&metric=ncloc#.svg file:https://sonarcloud.io/api/project_badges/measure?project=no.priv.bang.authservice%3Aauthservice&metric=bugs#.svg file:https://sonarcloud.io/api/project_badges/measure?project=no.priv.bang.authservice%3Aauthservice&metric=vulnerabilities#.svg file:https://sonarcloud.io/api/project_badges/measure?project=no.priv.bang.authservice%3Aauthservice&metric=code_smells#.svg file:https://sonarcloud.io/api/project_badges/measure?project=no.priv.bang.authservice%3Aauthservice&metric=coverage#.svg
Installing on karaf
/Note/: The instructions here don't describe a production enviroment, but they describe setting up something that will let the service be startet.
The webapp needs PostgreSQL running, with a database named "ukelonn" containing the table users, and a no-password authentication scheme.
Instructions:
- In bash, clone and build the authservice app:
#+BEGIN_EXAMPLE
mkdir -p ~/git/
cd ~/git/
git clone https://github.com/steinarb/authservice.git
cd ~/git/authservice/
mvn clean install
#+END_EXAMPLE
- [https://karaf.apache.org/manual/latest/quick-start.html][Follow the quick start guide to downloading, unpacking and starting apache karaf]]
- In the karaf shell, install the authservice feature repository
#+BEGIN_EXAMPLE
feature:repo-add mvn:no.priv.bang.authservice/authservice/LATEST/xml/features
#+END_EXAMPLE
- In the karaf shell, install the feature that installs the authorization service that is used by nginx (this feature installs a set of test users, roles and features)
#+BEGIN_EXAMPLE
feature:install authservice-with-derby-dbrealm-and-session
#+END_EXAMPLE
- Open a browser on the URL http://localhost:8181/authservice and do a login with a valid username/password combination (e.g. "admin/admin")
- Open a browser on the URL http://localhost:8181/authservice/check and verify that it doesn't return a 401 HTTP code
- Optionally install the user administration UI (not needed for using this service with nginx, but needed for administrating the access)
#+BEGIN_EXAMPLE
feature:install user-admin-with-derby
#+END_EXAMPLE
- Open a browser on the URL http://localhost:8181/authservice/useradmin and test adding/modifying users, roles and permissions
The webapp installed by the above installation instructions offers two URLs for use by the [[http://nginx.org/en/docs/http/ngx_http_auth_request_module.html][NGINX auth_request module]]:
- /auth which will just check the login state of Apache Shiro, returning the status code 401 for failure and 200 for success
- /login which contains a login form and will authenticate against Apache Shiro
The webapp is implemented as two servlets exposed as OSGi services, that will be picked up by the pax web whiteboard extender.
Installing and configuring nginx
Instructions:
- Install nginx with the auth module. On debian this is done with the command
#+BEGIN_EXAMPLE
apt-get update
apt-get install nginx-extras
#+END_EXAMPLE
- Add the following to the /etc/nginx/sites-available/default (adapt this to the actual server/site in use):
#+BEGIN_SRC conf
server {
listen 80 default_server;
listen [::]:80 default_server;
root /var/www/html;
# Add index.php to the list if you are using PHP
index index.html index.htm index.nginx-debian.html;
server_name _;
location /authservice {
auth_request off; # Necessary for REST API POST to work, shiro will handle authorization here
proxy_pass http://localhost:8181/authservice;
proxy_cookie_path ~^/authservice.*$ /;
proxy_set_header Host $host;
}
# Avoid browser attempt at fetching favicon.ico triggering a login and redirecting
# a 404 Not Found when there is no favicon.ico on the site (which is perferctly OK
# for both the site and the browser)
location /favicon.ico {
auth_request off;
}
location / {
# First attempt to serve request as file, then
# as directory, then fall back to displaying a 404.
try_files $uri $uri/ =404;
}
# Auth configuration
auth_request /authservice/check;
error_page 401 = @error401;
Installing and configuring postgresql
Installing and configuring apache karaf
Integrating with a Declarative Services (DS) web whiteboard application in karaf
# If the user is not logged in, redirect to authservice login URL, with redirect information
location @error401 {
add_header X-Original-URI "$scheme://$http_host$request_uri";
add_header Set-Cookie "NSREDIRECT=$scheme://$http_host$request_uri";
return 302 /authservice/login
}
}
#+END_SRC
/Note/: only command examples for debian/ubuntu/etc. are shown, but the overall steps should work on a lot of platforms
- Install PostgreSQL, as root do the following command:
#+BEGIN_EXAMPLE
apt-get install postgresql
#+END_EXAMPLE
- Add a PostgreSQL user named "karaf", as root do the following command
#+BEGIN_EXAMPLE
PGPASSWORD=karaf sudo -u postgres createuser karaf
#+END_EXAMPLE
/Note/: Replace the password in the PGPASSWORD environment variable with something other than the example and use that password in the karaf configuration
- Create an empty PostgreSQL database named "authservice" owned by user "karaf"
#+BEGIN_EXAMPLE
sudo -u postgres createdb -O karaf authservice
#+END_EXAMPLE
Instructions:
- Install apache karaf as a service, either using the karaf installation scripts or by using apt-get and the unofficial karaf deb package
- SSH in to the karaf console:
#+BEGIN_EXAMPLE
ssh -p 8101 karaf@localhost
#+END_EXAMPLE
The default password is "karaf" (without the quotes). It might be a good idea to change this. See the karaf documentation for how to change the password
- In the karaf console, do the following:
- Add connection configuration for the postgresql database:
#+BEGIN_EXAMPLE
config:edit no.priv.bang.authservice.db.postgresql.PostgresqlDatabase
config:property-set authservice.db.jdbc.url "jdbc:postgresql:///authservice"
config:property-set authservice.db.jdbc.user "karaf"
config:property-set authservice.db.jdbc.password "karaf"
config:update
#+END_EXAMPLE
/Note/: use the actual password given in the PGPASSWORD environment variable when creating the karaf user
- Install authservice from maven central:
#+BEGIN_EXAMPLE
feature:repo-add mvn:no.priv.bang.authservice/authservice/LATEST/xml/features
feature:install user-admin-with-postgresql
#+END_EXAMPLE
- Open a the nginx authservice URL in a web browser, e.g. https://myserver.com/authservice/ and:
- Log in as user "admin" with password "admin" (without the quotes)
- Click on the "User administration UI" link
- In the administration UI:
- Click on "Administrate users"
- Change the password of user "admin"
- Add users that are to be able to log in to nginx
/Note/: The nginx config provides only authentication for nginx, no authorization based on the combination of path and role or permission. Therefore there is no need to add roles to users that only needs to log in
Users that need to administrate other users, should get the useradmin role
- Add some links to the selfservice URLs from your website's top page (or whereever is convenient):
- Change password: https://myserver.com/authservice/password/
- Modify real namd and email: https://myserver.com/authservice/user
/Note/: only command examples for debian/ubuntu/etc. are shown, but the overall steps should work on a lot of platforms
Do the following steps:
- Install PostgreSQL, as root do the following command:
#+BEGIN_EXAMPLE
apt-get install postgresql
#+END_EXAMPLE
- Add a PostgreSQL user named "karaf", as root do the following command
#+BEGIN_EXAMPLE
PGPASSWORD=karaf sudo -u postgres createuser karaf
#+END_EXAMPLE
/Note/: Replace the password in the PGPASSWORD environment variable with something other than the example and use that password in the karaf configuration
- Create an empty PostgreSQL database named "authservice" owned by user "karaf"
#+BEGIN_EXAMPLE
sudo -u postgres createdb -O karaf authservice
#+END_EXAMPLE
- SSH into the karaf console and add connection configuration for the postgresql database with the following commands:
#+BEGIN_EXAMPLE
config:edit no.priv.bang.authservice.db.postgresql.PostgresqlDatabase
config:property-set authservice.db.jdbc.url "jdbc:postgresql:///authservice"
config:property-set authservice.db.jdbc.user "karaf"
config:property-set authservice.db.jdbc.password "karaf"
config:update
#+END_EXAMPLE
/Note/: use the actual password given in the PGPASSWORD environment variable when creating the karaf user
- Create a new DS component maven project, containing
- A src/main/feature/feature.xml template file, referencing the authservice feature repository and the authservice feature, e.g.:
#+BEGIN_SRC nxml
mvn:no.priv.bang.authservice/authservice/1.0.2/xml/features
user-admin-with-postgresql
#+END_SRC
- Add a shiro compile time dependency to the project's maven dependencies:
#+BEGIN_EXAMPLE
org.apache.shiro
shiro-core
1.3.2
provided
#+END_EXAMPLE
- A DS component exposing a ServletContextHelper service to the web whiteboard, e.g.:
#+BEGIN_SRC java
@Component(
property= {
HttpWhiteboardConstants.HTTP_WHITEBOARD_CONTEXT_NAME+"=ukelonn",
HttpWhiteboardConstants.HTTP_WHITEBOARD_CONTEXT_PATH+"=/ukelonn"},
service=ServletContextHelper.class,
immediate=true
)
public class UkelonnServletContextHelper extends ServletContextHelper { }
#+END_SRC
- A DS component exposing a Filter service to the web whiteboard, extending the AbstractShiroFilter, requiring shiro Realm and SessionDAO OSGi service injections, and configured using code (the shiro.ini mechanism doesn't work well in OSGi), eg.:
#+BEGIN_SRC java
@Component(
property= {
HttpWhiteboardConstants.HTTP_WHITEBOARD_FILTER_PATTERN+"=/*",
HttpWhiteboardConstants.HTTP_WHITEBOARD_CONTEXT_SELECT + "=(" + HttpWhiteboardConstants.HTTP_WHITEBOARD_CONTEXT_NAME +"=ukelonn)",
"servletNames=ukelonn"},
service=Filter.class,
immediate=true
)
public class UkelonnShiroFilter extends AbstractShiroFilter { // NOSONAR
private Realm realm;
private SessionDAO session;
private static final Ini INI_FILE = new Ini();
static {
// Can't use the Ini.fromResourcePath(String) method because it can't find "shiro.ini" on the classpath in an OSGi context
INI_FILE.load(UkelonnShiroFilter.class.getClassLoader().getResourceAsStream("shiro.ini"));
}
@Reference
public void setRealm(Realm realm) {
this.realm = realm;
}
@Reference
public void setSession(SessionDAO session) {
this.session = session;
}
@Activate
public void activate() {
WebIniSecurityManagerFactory securityManagerFactory = new WebIniSecurityManagerFactory(INI_FILE);
DefaultWebSecurityManager securityManager = (DefaultWebSecurityManager) securityManagerFactory.createInstance();
DefaultWebSessionManager sessionmanager = new DefaultWebSessionManager();
sessionmanager.setSessionDAO(session);
securityManager.setSessionManager(sessionmanager);
setSecurityManager(securityManager);
securityManager.setRealm(realm);
IniFilterChainResolverFactory filterChainResolverFactory = new IniFilterChainResolverFactory(INI_FILE, securityManagerFactory.getBeans());
FilterChainResolver resolver = filterChainResolverFactory.createInstance();
setFilterChainResolver(resolver);
}
}
#+END_SRC
- A shiro.ini resource containing a [urls] section providing access to various path, e.g:
#+BEGIN_EXAMPLE
[main]
authc.loginUrl = /login
[users]
[urls]
/ = authc
/user* = user
/admin/** = roles[administrator]
/api/login = anon
/api/registerpayment = roles[administrator]
/api/job/update = roles[administrator]
/api/admin/** = roles[administrator]
/api/** = authc
/performedjobs = authc
/performedpayments = authc
#+END_EXAMPLE
- Something listening to the /login path inside the context provided by the WebContextHelper (i.e. /ukelonn/login in this example) and handling login. "Something" could be a servlet or a JAX-RS resource. An example of a JAX-RS resource to handle login, is this resource, which when receiving a GET returns an HTML page with a login form, and on receiving a POST from the form, performs the login:
#+BEGIN_SRC java
@Path("")
public class LoginResource {
@GET
@Path("/login")
@Produces(MediaType.TEXT_HTML)
public InputStream getLogin() {
return getClass().getClassLoader().getResourceAsStream("web/login.html");
}
@POST
@Path("/login")
@Consumes(MediaType.APPLICATION_FORM_URLENCODED)
@Produces("text/html")
public Response postLogin(@FormParam("username") String username, @FormParam("password") String password) {
Subject subject = SecurityUtils.getSubject();
UsernamePasswordToken token = new UsernamePasswordToken(username, password.toCharArray(), true);
try {
subject.login(token);
return Response.status(Response.Status.FOUND).entity("Login successful!").build();
} catch(UnknownAccountException e) {
return Response.status(Response.Status.UNAUTHORIZED).entity("Unknown account")).build();
} catch (IncorrectCredentialsException e) {
return Response.status(Response.Status.UNAUTHORIZED).entity("Wrong password")).build();
} catch (LockedAccountException e) {
return Response.status(Response.Status.UNAUTHORIZED).entity("Account is locked")).build();
} catch (AuthenticationException e) {
return Response.status(Response.Status.UNAUTHORIZED).entity("Unable to log in")).build();
} catch (Exception e) {
throw new InternalServerErrorException();
} finally {
token.clear();
}
}
}
#+END_SRC
Note! if the user logs in via the login form on the authservice path on the same karaf server, the user will be logged into your application as well.
Various ways of integrating with other webapps in karaf
There are several ways for a webapp to interact with authservice:
- Install authservice separately and add OSGi service injections for shiro Realm and Session (all user administration done in the authservice webapplication)
- Add the features for the liquibase database setup and the shiro Realm and Session and provide the necessary tables from a different web application's database
- Add the features for the authservice UserManagementService implementation, as well as the features for Realm and Session and and implement a user management GUI and webservice on top of the UserManagementService
...or various permutations of the above. With ukelonn I plan to add the authservice tables to the ukelonn database, and then let the ukelonn database provide the database for authservice itself. I have made a first step in the direction of authservice integration by basing ukelonn's user management on the UserManagementService OSGi service, so that it later can be replaced by the authservice implementation of the service.
Integrating with other databases than PostgreSQL
Short story: it should be possible. It should possible to use blank JDBC database that can be connected to with a combination of a JDBC url and username and password.
Possible approach:
- Copy the authservice postgresql maven project
- Remove the postgresql compile scope dependency from the pom.xml (this dependency adds a bundle dependency to the feature file)
- Add a dependency to the desired JDBC driver. It has to be something that provides a DataSourceFactory OSGi service. If the database system's JDBC driver doesn't provide a DataSourceFactory OSGi service (like e.g. PostgreSQL does), check if the pax-jdbc project have something appropriate
Release history
Date |
Version |
Comment |
[2019-04-15] |
1.0.2 |
Upgrade apache shiro from version 1.3.1 to version 1.3.2 |
[2019-04-12] |
1.0.1 |
Avoid constraint name conflict caused by copy-paste from ukelonn liquibase schema, fix aggregate javadoc, ensure user admin with role useradmin is always created if not present |
[2019-04-02] |
1.0.0 |
Initial release |
License
This software is licensed under Apache Public License v 2.0.
See the LICENSE file for the full details.