Interface AtlassianHostRestClients


  • public interface AtlassianHostRestClients
    A helper class for obtaining preconfigured RestTemplates to make authenticated requests to Atlassian hosts.

    JWT

    To make requests using JWT, the add-on must specify the authentication type jwt in its add-on descriptor.

    To obtain a RestTemplate using JWT authentication, use authenticatedAsAddon():

     @Autowired
     private AtlassianHostRestClients restClients;
    
     public void makeRequest() {
         restClients.authenticatedAsAddon().getForObject(...);
     }

    OAuth 2.0 - JWT Bearer Token

    To make requests using OAuth 2.0, the add-on must request the ACT_AS_USER scope in its add-on descriptor.

    To obtain a RestTemplate using OAuth 2.0 authentication, use authenticatedAsHostActor() or authenticatedAs(AtlassianHostUser):

     @Autowired
     private AtlassianHostRestClients restClients;
    
     public void makeRequest() {
         restClients.authenticatedAsHostActor().getForObject(...);
     }
    Since:
    1.1.0
    • Method Detail

      • authenticatedAsAddon

        RestTemplate authenticatedAsAddon()
        Returns a RestTemplate for making requests to Atlassian hosts using JWT authentication. The principal of the request is the add-on.

        During processing of a request from an Atlassian host, relative URLs can be used to make requests to the current host.

        When a request is made to an absolute URL, the request URL is used to resolve the destination Atlassian host. If no host matches, the request is not signed.

        Returns:
        a REST client for JWT authentication
        See Also:
        authenticatedAsAddon(AtlassianHost)
      • authenticatedAsAddon

        RestTemplate authenticatedAsAddon​(AtlassianHost host)
        Returns a RestTemplate for making requests to Atlassian hosts using JWT authentication. The principal of the request is the add-on.

        Relative URLs can be used to make requests to the given host.

        When a request is made to an absolute URL, the URL must match the base URL of the given host.

        Parameters:
        host - the host to which the request should be made
        Returns:
        a REST client for JWT authentication
        See Also:
        authenticatedAsAddon()
      • createJwt

        String createJwt​(HttpMethod method,
                         URI uri)
        Creates a JSON Web Token for use when the RestTemplate provided by authenticatedAsAddon() cannot be used to make requests to Atlassian hosts, such as when using Jersey.
         WebTarget webTarget = ClientBuilder.newClient().target(host.getBaseUrl()).path(...);
         String jwt = atlassianHostRestClients.createJwt(HttpMethod.GET, webTarget.getUri());
         Response response = webTarget.request().header("Authorization", "JWT " + jwt).get();

        NOTE: Whenever possible, use of authenticatedAsAddon() is recommended over use of this method.

        The created JWT is restricted for use with the given HTTP method and request URL.

        The request URL is used to resolve the destination Atlassian host. If no host matches, an IllegalArgumentException is thrown.

        Parameters:
        method - the HTTP method of the request to be authenticated
        uri - the absolute URL of the request to be authenticated
        Returns:
        a JWT for use when authenticating as the add-on
        Throws:
        IllegalArgumentException - if the URL did not have the base URL of any installed host
        Since:
        1.3.0
        See Also:
        authenticatedAsAddon(), authenticatedAsAddon(AtlassianHost)
      • authenticatedAsHostActor

        OAuth2RestTemplate authenticatedAsHostActor()
        Returns a RestTemplate for making requests to the currently authenticated Atlassian host using OAuth 2.0 JWT Bearer Token authentication. The principal of the request is the currently authenticated user.

        On first invocation, the OAuth2RestTemplate will request an access token from Atlassian's authorization server. The access token will then be cached for further use. Once the token has expired, a new token will be fetched transparently. Additionally, the OAuth2RestTemplate for a particular host user is cached between requests using Spring Caching.

        Returns:
        a REST client for OAuth 2.0 JWT Bearer Token authentication
        See Also:
        Spring Security OAuth 2, authenticatedAs(AtlassianHostUser)
      • authenticatedAs

        OAuth2RestTemplate authenticatedAs​(AtlassianHostUser hostUser)
        Returns a RestTemplate for making requests to the given Atlassian host using OAuth 2.0 JWT Bearer Token authentication. The principal of the request is the given user.

        On first invocation, the OAuth2RestTemplate will request an access token from Atlassian's authorization server. The access token will then be cached for further use. Once the token has expired, a new token will be fetched transparently. Additionally, the OAuth2RestTemplate for a particular host user is cached between requests using Spring Caching.

        Parameters:
        hostUser - the host to which the request should be made, and the user principal
        Returns:
        a REST client for OAuth 2.0 JWT Bearer Token authentication
        See Also:
        Spring Security OAuth 2, authenticatedAsHostActor()