Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions src/main/java/au/com/southsky/jfreesane/SaneOption.java
Original file line number Diff line number Diff line change
Expand Up @@ -782,13 +782,13 @@ private static ControlOptionResult fromSession(SaneSession session)
String resource = stream.readString();

if (!resource.isEmpty()) {
session.authorize(resource);
status = stream.readWord();
if (!session.authorize(resource)) {
throw new SaneException(SaneStatus.STATUS_ACCESS_DENIED);
}

status = stream.readWord();
info = stream.readWord().integerValue();

type = SaneEnums.valueOf(OptionValueType.class, stream.readWord().integerValue());

valueSize = stream.readWord().integerValue();

// read the pointer
Expand Down
44 changes: 22 additions & 22 deletions src/main/java/au/com/southsky/jfreesane/SaneSession.java
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,11 @@ public SanePasswordProvider getPasswordProvider() {

/**
* Sets the {@link SanePasswordProvider password provider} to use if the SANE daemon asks for
* credentials when accessing a resource.
* credentials when accessing a resource. Throws {@link NullPointerException} if
* {@code passwordProvider} is {@code null}.
*/
public void setPasswordProvider(SanePasswordProvider passwordProvider) {
this.passwordProvider = passwordProvider;
this.passwordProvider = Preconditions.checkNotNull(passwordProvider);
}

/**
Expand Down Expand Up @@ -191,7 +192,9 @@ SaneDeviceHandle openDevice(SaneDevice device) throws IOException, SaneException
}

if (!resource.isEmpty()) {
authorize(resource);
if (!authorize(resource)) {
throw new SaneException(SaneStatus.STATUS_ACCESS_DENIED);
}
status = inputStream.readWord();
handle = inputStream.readWord();
resource = inputStream.readString();
Expand Down Expand Up @@ -227,7 +230,9 @@ BufferedImage acquireImage(SaneDevice device, ScanListener listener)
}

if (!resource.isEmpty()) {
authorize(resource);
if (!authorize(resource)) {
throw new SaneException(SaneStatus.STATUS_ACCESS_DENIED);
}
int status = inputStream.readWord().integerValue();
port = inputStream.readWord().integerValue();
byteOrder = inputStream.readWord();
Expand Down Expand Up @@ -337,32 +342,27 @@ private void initSane() throws IOException {
*
* @throws IOException if an error occurs while communicating with the SANE daemon
*/
void authorize(String resource) throws IOException {
boolean authorize(String resource) throws IOException {
if (passwordProvider == null) {
throw new IOException(
"Authorization failed - no password provider present "
+ "(you must call setPasswordProvider)");
}

if (!passwordProvider.canAuthenticate(resource)) {
// the password provider has indicated that there's no way it can provide
// credentials for this request.
throw new IOException(
"Authorization failed - the password provider is "
+ "unable to provide a password for the resource ["
+ resource
+ "]");
}
if (passwordProvider.canAuthenticate(resource)) {
// RPC code FOR SANE_NET_AUTHORIZE
outputStream.write(SaneRpcCode.SANE_NET_AUTHORIZE);
outputStream.write(resource);
outputStream.write(passwordProvider.getUsername(resource));
writePassword(resource, passwordProvider.getPassword(resource));
outputStream.flush();

// RPC code FOR SANE_NET_AUTHORIZE
outputStream.write(SaneRpcCode.SANE_NET_AUTHORIZE);
outputStream.write(resource);
outputStream.write(passwordProvider.getUsername(resource));
writePassword(resource, passwordProvider.getPassword(resource));
outputStream.flush();
// Read dummy reply and discard (according to the spec, it is unused).
inputStream.readWord();
return true;
}

// Read dummy reply and discard (according to the spec, it is unused).
inputStream.readWord();
return false;
}

/**
Expand Down
Loading