This contains the library to do authentication and authorization for all Project EPIC microservices.
- Configure your local maven installation to use GitHub to push the package (you can learn how to do so here)
- Update version in
pom.xml
mvn deploy
- Add dependency on
pom.xml
.
<dependency>
<groupId>edu.colorado.cs.epic</groupId>
<artifactId>authlib</artifactId>
<version>1.1.0</version>
</dependency>
- To install, add the following code on your Application run method:
AddAuthToEnv.register(environment);
Where environment
is your Environment parameter. To make testing easier, you can add a production variable on your configuration file such that it can be turned on and off without needing to recompile. Example:
AddAuthToEnv.register(environment, configuration.getProduction());
Include an Authorization header to the request with the following format: Bearer ACCESS_TOKEN
. Where ACCESS_TOKEN
is the jwt obtained when logged in on Firebase.
Make sure the Authorization header is allowed in your CORS configuration.
cors.setInitParameter("allowedOrigins", "*");
cors.setInitParameter("allowedHeaders", "X-Requested-With,Authorization,Content-Type,Accept,Origin");
cors.setInitParameter("allowedMethods", "OPTIONS,GET,PUT,POST,DELETE,HEAD");
Accepted annotations to protect your resources:
@RolesAllowed("ADMIN")
: Protects method against users logged in but not authorized by an internal member.@PermitAll
: Checks if user exists and is logged in. Any user logged in is allowed to access the resource.
To access the logged in user from a resource method, you can add the following parameter:
@Auth Optional<FirebaseUser> user
More information available: https://www.dropwizard.io/1.3.9/docs/manual/auth.html