
com.atlassian.connect.spring.AtlassianHostUser Maven / Gradle / Ivy
package com.atlassian.connect.spring;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.User;
import java.util.Objects;
import java.util.Optional;
/**
* The Spring Security
* authentication principal for requests coming from an Atlassian host application in which the
* add-on is installed.
*
* This class is the Atlassian Connect equivalent of Spring Security's {@link User} and can be resolved as a Spring
* MVC controller argument by using the {@link AuthenticationPrincipal} annotation:
*
@Controller
* public class HelloWorldController {
*
* @RequestMapping(value = "/helloWorld", method = GET)
* public String (@AuthenticationPrincipal AtlassianHostUser hostUser) {
* ...
* }
* }
*
* Outside of the context of a controller, the current authentication principal can be obtained programmatically via
* {@link SecurityContextHolder}:
*
Optional<AtlassianHostUser> optionalHostUser = Optional.ofNullable(SecurityContextHolder.getContext().getAuthentication())
* .map(Authentication::getPrincipal)
* .filter(AtlassianHostUser.class::isInstance)
* .map(AtlassianHostUser.class::cast);
*
* @since 1.0.0
*/
public final class AtlassianHostUser {
private AtlassianHost host;
private Optional userKey;
/**
* Creates a new empty Atlassian host principal (used for deserialization).
*/
protected AtlassianHostUser() {}
/**
* Creates a new Atlassian host principal.
*
* @param host the host from which the request originated
* @param optionalUserKey the key of the user on whose behalf a request was made (optional)
*/
public AtlassianHostUser(AtlassianHost host, Optional optionalUserKey) {
this.host = host;
this.userKey = optionalUserKey;
}
/**
* Returns the host from which the request originated.
*
* @return the Atlassian host
*/
public AtlassianHost getHost() {
return host;
}
/**
* The the key of the user on whose behalf a request was made.
*
* @return the user key
*/
public Optional getUserKey() {
return userKey;
}
/**
* {@inheritDoc}
*/
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
AtlassianHostUser that = (AtlassianHostUser) o;
return Objects.equals(host, that.host) && Objects.equals(userKey, that.userKey);
}
/**
* {@inheritDoc}
*/
@Override
public int hashCode() {
return Objects.hash(host, userKey);
}
/**
* {@inheritDoc}
*/
@Override
public String toString() {
return String.format("AtlassianHostUser{host=%s, userKey='%s'}", host, userKey.orElse("null"));
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy