File Exchange Pick of the Week

Our best user submissions

Get Authentication

Sean's pick this week is GetAuthentication by Stefan.

I was recently asked about a simple way to password protect an executable compiled with MATLAB Compiler. The customer's goal was that this would be on a shared machine and only authorized users would be able to use the executable.

It occurred to me that I frequently use a MATLAB-based password box every time I upload a file to our FTP or SFTP site to share with customers. Digging into the code there, I discovered this file and was excited to see it publicly available on the File Exchange.

To use it to protect your executable, simply call it in a wrapper function to decide whether to open the main executable or quit. Here's a simple example that gives access to my four dogs to see a picture of themselves.

function authenticateApp()
% This function authenticates a username and password before opening an
% application.

    % Username and password for each of my dogs
    unames = {'Zoey', 'Mya', 'Lexi', 'Lilly'};
    passwords  = {'MilkB0ne', 'Dent@Stix', 'Pupp3roni', 's0ck$'};

    % Try three times
    for ii = 1:3
        % Get authentication
        [user, pass] = GetAuthentication();
        
        % Does a username match?
        useridx = ismember(unames,user);
        if nnz(useridx)
            % If yes, does the password match for that user?
            if isequal(useridx, ismember(passwords,pass))
                
                % Open application here:
                imshow('fourdogs.jpg')
                
                break
            end
        end
    end            
end

Calling it:

authenticateApp

Compiling

Now if you want to compile this, make the authentication wrapper the main function and your app will have this protection around it.

Suggestion

My only suggestion would be for GetAuthentication to pass back a third output argument about whether the user hit the cancel button. Right now, if cancel is hit, it just returns empty for password and username. This is ambiguous to hitting okay with empty password and username which is a challenge to handle if you want to use it in a loop like I did.

Comments

Give it a try and let us know what you think here or leave a comment for Stefan.




Published with MATLAB® R2016b

|
  • print

评论

要发表评论,请点击 此处 登录到您的 MathWorks 帐户或创建一个新帐户。