Support bearer token

May be use as follows:
```
ollamaAPI.setBasicAuth(new BasicAuth() {
	@Override
	public String getBasicAuthHeaderValue() { return "Bearer [sometext]"; }
});
```

Signed-off-by: Sven Strickroth <email@cs-ware.de>
This commit is contained in:
Sven Strickroth 2025-03-10 14:39:54 +01:00
parent 7ef859bba5
commit 3a792090e2
3 changed files with 24 additions and 40 deletions

View File

@ -109,6 +109,10 @@ public class OllamaAPI {
this.basicAuth = new BasicAuth(username, password); this.basicAuth = new BasicAuth(username, password);
} }
public void setBasicAuth(BasicAuth basicAuth) {
this.basicAuth = basicAuth;
}
/** /**
* API to check the reachability of Ollama server. * API to check the reachability of Ollama server.
* *
@ -1083,21 +1087,11 @@ public class OllamaAPI {
private HttpRequest.Builder getRequestBuilderDefault(URI uri) { private HttpRequest.Builder getRequestBuilderDefault(URI uri) {
HttpRequest.Builder requestBuilder = HttpRequest.newBuilder(uri).header("Content-Type", "application/json").timeout(Duration.ofSeconds(requestTimeoutSeconds)); HttpRequest.Builder requestBuilder = HttpRequest.newBuilder(uri).header("Content-Type", "application/json").timeout(Duration.ofSeconds(requestTimeoutSeconds));
if (isBasicAuthCredentialsSet()) { if (isBasicAuthCredentialsSet()) {
requestBuilder.header("Authorization", getBasicAuthHeaderValue()); requestBuilder.header("Authorization", basicAuth.getBasicAuthHeaderValue());
} }
return requestBuilder; return requestBuilder;
} }
/**
* Get basic authentication header value.
*
* @return basic authentication header value (encoded credentials)
*/
private String getBasicAuthHeaderValue() {
String credentialsToEncode = basicAuth.getUsername() + ":" + basicAuth.getPassword();
return "Basic " + Base64.getEncoder().encodeToString(credentialsToEncode.getBytes());
}
/** /**
* Check if Basic Auth credentials set. * Check if Basic Auth credentials set.
* *

View File

@ -1,5 +1,7 @@
package io.github.ollama4j.models.request; package io.github.ollama4j.models.request;
import java.util.Base64;
import lombok.AllArgsConstructor; import lombok.AllArgsConstructor;
import lombok.Data; import lombok.Data;
import lombok.NoArgsConstructor; import lombok.NoArgsConstructor;
@ -10,4 +12,14 @@ import lombok.NoArgsConstructor;
public class BasicAuth { public class BasicAuth {
private String username; private String username;
private String password; private String password;
/**
* Get basic authentication header value.
*
* @return basic authentication header value (encoded credentials)
*/
public String getBasicAuthHeaderValue() {
final String credentialsToEncode = this.getUsername() + ":" + this.getPassword();
return "Basic " + Base64.getEncoder().encodeToString(credentialsToEncode.getBytes());
}
} }

View File

@ -1,26 +1,14 @@
package io.github.ollama4j.models.request; package io.github.ollama4j.models.request;
import io.github.ollama4j.OllamaAPI; import java.net.URI;
import io.github.ollama4j.exceptions.OllamaBaseException; import java.net.http.HttpRequest;
import io.github.ollama4j.models.response.OllamaErrorResponse; import java.time.Duration;
import io.github.ollama4j.models.response.OllamaResult;
import io.github.ollama4j.utils.OllamaRequestBody;
import io.github.ollama4j.utils.Utils;
import lombok.Getter;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import java.io.BufferedReader; import io.github.ollama4j.OllamaAPI;
import java.io.IOException; import lombok.Getter;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.nio.charset.StandardCharsets;
import java.time.Duration;
import java.util.Base64;
/** /**
* Abstract helperclass to call the ollama api server. * Abstract helperclass to call the ollama api server.
@ -59,21 +47,11 @@ public abstract class OllamaEndpointCaller {
.header("Content-Type", "application/json") .header("Content-Type", "application/json")
.timeout(Duration.ofSeconds(this.requestTimeoutSeconds)); .timeout(Duration.ofSeconds(this.requestTimeoutSeconds));
if (isBasicAuthCredentialsSet()) { if (isBasicAuthCredentialsSet()) {
requestBuilder.header("Authorization", getBasicAuthHeaderValue()); requestBuilder.header("Authorization", this.basicAuth.getBasicAuthHeaderValue());
} }
return requestBuilder; return requestBuilder;
} }
/**
* Get basic authentication header value.
*
* @return basic authentication header value (encoded credentials)
*/
protected String getBasicAuthHeaderValue() {
String credentialsToEncode = this.basicAuth.getUsername() + ":" + this.basicAuth.getPassword();
return "Basic " + Base64.getEncoder().encodeToString(credentialsToEncode.getBytes());
}
/** /**
* Check if Basic Auth credentials set. * Check if Basic Auth credentials set.
* *