diff --git a/src/main/java/org/hisp/dhis/Dhis2.java b/src/main/java/org/hisp/dhis/Dhis2.java index 63d66197..bc107ffa 100644 --- a/src/main/java/org/hisp/dhis/Dhis2.java +++ b/src/main/java/org/hisp/dhis/Dhis2.java @@ -79,6 +79,7 @@ import org.hisp.dhis.api.LogLevel; import org.hisp.dhis.auth.AccessTokenAuthentication; import org.hisp.dhis.auth.BasicAuthentication; +import org.hisp.dhis.auth.BearerAuthentication; import org.hisp.dhis.auth.CookieAuthentication; import org.hisp.dhis.auth.NoAuthentication; import org.hisp.dhis.model.AnalyticsTableHook; @@ -237,6 +238,23 @@ public static Dhis2 withAccessTokenAuth(String url, String accessToken) { return new Dhis2(new Dhis2Config(url, new AccessTokenAuthentication(accessToken))); } + /** + * Creates a {@link Dhis2} instance configured for OAuth2 bearer token authentication. The token + * is sent as {@code Authorization: Bearer }, suitable for OAuth2 access tokens (as opposed + * to {@link #withAccessTokenAuth(String, String)}, which uses the {@code ApiToken} scheme for + * personal access tokens). + * + * @param url the URL to the DHIS2 instance, do not include the {@code /api} part or a trailing + * {@code /}. + * @param accessToken the OAuth2 bearer access token. + * @return a {@link Dhis2} instance. + */ + public static Dhis2 withBearerTokenAuth(String url, String accessToken) { + Verify.notEmpty(url, "URL must be provided"); + Verify.notEmpty(accessToken, "Access token must be provided"); + return new Dhis2(new Dhis2Config(url, new BearerAuthentication(accessToken))); + } + /** * Creates a {@link Dhis2} instance configured for cookie authentication. * diff --git a/src/main/java/org/hisp/dhis/auth/BearerAuthentication.java b/src/main/java/org/hisp/dhis/auth/BearerAuthentication.java new file mode 100644 index 00000000..8d67b998 --- /dev/null +++ b/src/main/java/org/hisp/dhis/auth/BearerAuthentication.java @@ -0,0 +1,56 @@ +/* + * Copyright (c) 2004-2024, University of Oslo + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * Neither the name of the HISP project nor the names of its contributors may + * be used to endorse or promote products derived from this software without + * specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR + * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON + * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ +package org.hisp.dhis.auth; + +import java.io.Serializable; +import lombok.Getter; +import lombok.NonNull; +import lombok.RequiredArgsConstructor; +import org.apache.hc.core5.http.HttpHeaders; +import org.hisp.dhis.util.HttpUtils; + +/** + * Class representing OAuth2 bearer token authentication. Sends the token as {@code Authorization: + * Bearer }, suitable for OAuth2 access tokens, as opposed to {@link + * AccessTokenAuthentication} which uses the {@code ApiToken} scheme for personal access tokens. + */ +@Getter +@RequiredArgsConstructor +public class BearerAuthentication implements Authentication, Serializable { + @NonNull private final String accessToken; + + @Override + public String getHttpHeaderAuthName() { + return HttpHeaders.AUTHORIZATION; + } + + @Override + public String getHttpHeaderAuthValue() { + return HttpUtils.getBearerTokenAuthString(accessToken); + } +}