}
}
}
AuthenticatedUser authentication = null;
if (authRequest != null) {
authentication = loginService.authenticate(authRequest, clientCertificate);
}
if (authentication == null) {
throw new WebApplicationException(Status.UNAUTHORIZED);
}
V2AuthResponse response = new V2AuthResponse();
Access access = response.access = new Access();
V2Token token = access.token = new V2Token();
ProjectData project = authentication.getProject();
// We never pass domain; we can't build a domain token with V2
TokenInfo tokenInfo = loginService.buildTokenInfo(authentication);
token.expires = TokenAuth.getExpiration(tokenInfo);
token.id = tokenService.encodeToken(tokenInfo);
if (project != null) {
Tenant tenant = new Tenant();
tenant.id = "" + project.getId();
tenant.name = project.getName();
token.tenant = tenant;
}
if (project != null) {
List<Service> v3Services = loginService.buildServiceMap(getBaseUrl(), project);
access.services = toV2Services(project, v3Services);
}
User user = access.user = new User();
user.id = "" + authentication.getUserData().getId();
user.name = authentication.getUserData().getName();
if (authentication.getProjectRoleIds() != null) {
user.roles = Lists.newArrayList();
for (long roleId : authentication.getProjectRoleIds()) {
RoleData role = identityService.findRole(roleId);
if (role == null) {
throw new IllegalStateException();
}