PDA

View Full Version : Finder password dialog with Java




Transporteur
Nov 2, 2009, 05:32 PM
Hi guys

I'm doing some Java IO stuff at the moment, which is fine if you are working on a single computer with read/write access to every folder.

Assuming that I want to copy a file to a network folder that is mounted using the guest account, is there any option to open the OS X password dialog that asks for the correct username and password in order to get full access to that folder?

I don't want to mount the folders first using the finder, so a solution within my Java program would be nice.



chown33
Nov 2, 2009, 06:14 PM
The first thing that comes to mind is to exec the authopen command.

http://developer.apple.com/mac/library/documentation/Darwin/Reference/ManPages/man1/authopen.1.html

I recommend you try it first in a Terminal shell window.

Also, be sure to use the String[] form of exec(), not the single-String form.

Transporteur
Nov 3, 2009, 06:45 AM
Unfortunately I'm absolutely not familiar with the Terminal, especially when it comes to implement such commands in my Java code.

Isn't there a command Apple offers to open that Finder dialog that asks for permission to change the folder?

robbieduncan
Nov 3, 2009, 06:55 AM
The "Finder Dialog" is provided by Authorisation Services (http://developer.apple.com/mac/library/documentation/Security/Conceptual/authorization_concepts/01introduction/introduction.html) in OSX. It enables an application to get permission to and then actually perform a privileged action. In a normal OSX app you would just use this. There is no direct Java equivalent as this is platform specific. I would recommend writing a small tool that is OSX native in a native language (like Objective-C) to get access to this.

chown33
Nov 3, 2009, 10:12 AM
Google for:
authorization mac os x java

Top hit:
http://www.amug.org/~glguerin/sw/#authkit

lee1210
Nov 3, 2009, 10:25 AM
Authorization Services are normally the "right answer", but in this case the OP wants to get a username and password for a remote system. Escalating to a privileged local account isn't going to buy access to these remote resources. I'm unfamiliar with java gui programming, but i can only assume that there is a form of input that obscures what is being typed with bullets, etc. I would just get the password in there, username in a regular box.

Authorization Services are appropriate for local privilege escalation, and users will know what's going on... but Cyberduck, Transmit, etc. that need to connect to an FTP server just have a panel/text fields for you to enter authentication information.

-Lee

robbieduncan
Nov 3, 2009, 10:26 AM
Ah, yes, I see. I mis-read the original question. Once you get the username and password what are you going to do? Mount manually via a command-line tool?

Transporteur
Nov 3, 2009, 11:30 AM
I finally found it by accident last night at 4am.

Instead of choosing a file/directory using the java.swing JFileChooser, you have to use java.awt FileDialog.

Not only does it look like Apples native dialog, it supports Apples 'connect as...' feature which is exactly what I am looking for.

Thank you anyway for your support. Appreciate it!

robbieduncan
Nov 3, 2009, 11:31 AM
That may be because Apple supply a custom AWT for Mac OSX...

Transporteur
Nov 3, 2009, 11:41 AM
I didn't know that.
I am not a subscribed developer yet (free though, but that doesn't really count), so it's pretty hard to get proper information, or do I miss a source on the Apple developers site?

robbieduncan
Nov 3, 2009, 11:44 AM
I didn't know that.
I am not a subscribed developer yet (free though, but that doesn't really count), so it's pretty hard to get proper information, or do I miss a source on the Apple developers site?

It's something I remember from way back when people wanted to get the OSX L&F from Java on the Mac on other platforms and it became clear that Sun couldn't do that as Apple had written it/owned the copyright to the Aqua L&F for AWT.

Transporteur
Nov 3, 2009, 11:57 AM
That definitely makes sense.
I just wasn't aware of that because I'm usually coding with Windows and there isn't any 'connect as...' feature implemented in the AWT FileDialog.

Is there any documentation about Apples own AWT?

pikester
Nov 3, 2009, 12:23 PM
You can use your own GUI to get a username, password, and (optionally) the domain. You can then pass this information to a Mac utility to mount the drive. Assuming that you are trying to mount a SMB drive (i.e. a Windows mount), you can try something like this:

// Mount the drive
String[] cmd1 = {"/sbin/mount_smbfs", "//domain;username:password@server/share", "/path/to/dir"};
Process p1 = Runtimee.getRuntime().exec(cmd1);
p1.waitFor();

// Copy the file...

// Unmount
String cmd2 = {"/sbin/umount", "/path/to/dir"};
Process p2 = Runtime.getRuntime().exec(cmd2);
p2.waitFor();

Note: If you don't need the domain, just leave off 'domain;' from the command.

Transporteur
Nov 3, 2009, 12:33 PM
That's very useful when it comes to port my program to the Windows platform as it's awt does not support the feature.
Thank you very much!