{"id":7432,"date":"2016-06-24T09:52:40","date_gmt":"2016-06-24T13:52:40","guid":{"rendered":"https:\/\/blogs.mathworks.com\/pick\/?p=7432"},"modified":"2016-06-24T09:53:41","modified_gmt":"2016-06-24T13:53:41","slug":"get-authentication","status":"publish","type":"post","link":"https:\/\/blogs.mathworks.com\/pick\/2016\/06\/24\/get-authentication\/","title":{"rendered":"Get Authentication"},"content":{"rendered":"<div xmlns:mwsh=\"https:\/\/www.mathworks.com\/namespace\/mcode\/v1\/syntaxhighlight.dtd\" class=\"content\">\r\n   <introduction>\r\n      <p><a href=\"https:\/\/www.mathworks.com\/matlabcentral\/profile\/authors\/3208495\">Sean<\/a>'s pick this week is <a href=\"https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/23045-getauthentication\">GetAuthentication<\/a> by <a href=\"https:\/\/www.mathworks.com\/matlabcentral\/profile\/authors\/559000\">Stefan<\/a>.\r\n      <\/p>\r\n   <\/introduction>\r\n   <p>I was recently asked about a simple way to password protect an executable compiled with <a href=\"https:\/\/www.mathworks.com\/products\/compiler\/\">MATLAB Compiler<\/a>.  The customer's goal was that this would be on a shared machine and only authorized users would be able to use the executable.\r\n   <\/p>\r\n   <p>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\r\n      share with customers. Digging into the code there, I discovered this file and was excited to see it publicly available on\r\n      the File Exchange.\r\n   <\/p>\r\n   <p>To use it to protect your executable, simply call it in a wrapper function to decide whether to open the main executable or\r\n      quit.  Here's a simple example that gives access to my four dogs to see a picture of themselves.\r\n   <\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">\r\n<span style=\"color: #0000FF\">function<\/span> authenticateApp()\r\n<span style=\"color: #228B22\">% This function authenticates a username and password before opening an<\/span>\r\n<span style=\"color: #228B22\">% application.<\/span>\r\n\r\n    <span style=\"color: #228B22\">% Username and password for each of my dogs<\/span>\r\n    unames = {<span style=\"color: #A020F0\">'Zoey'<\/span>, <span style=\"color: #A020F0\">'Mya'<\/span>, <span style=\"color: #A020F0\">'Lexi'<\/span>, <span style=\"color: #A020F0\">'Lilly'<\/span>};\r\n    passwords  = {<span style=\"color: #A020F0\">'MilkB0ne'<\/span>, <span style=\"color: #A020F0\">'Dent@Stix'<\/span>, <span style=\"color: #A020F0\">'Pupp3roni'<\/span>, <span style=\"color: #A020F0\">'s0ck$'<\/span>};\r\n\r\n    <span style=\"color: #228B22\">% Try three times<\/span>\r\n    <span style=\"color: #0000FF\">for<\/span> ii = 1:3\r\n        <span style=\"color: #228B22\">% Get authentication<\/span>\r\n        [user, pass] = GetAuthentication();\r\n        \r\n        <span style=\"color: #228B22\">% Does a username match?<\/span>\r\n        useridx = ismember(unames,user);\r\n        <span style=\"color: #0000FF\">if<\/span> nnz(useridx)\r\n            <span style=\"color: #228B22\">% If yes, does the password match for that user?<\/span>\r\n            <span style=\"color: #0000FF\">if<\/span> isequal(useridx, ismember(passwords,pass))\r\n                \r\n                <span style=\"color: #228B22\">% Open application here:<\/span>\r\n                imshow(<span style=\"color: #A020F0\">'fourdogs.jpg'<\/span>)\r\n                \r\n                <span style=\"color: #0000FF\">break<\/span>\r\n            <span style=\"color: #0000FF\">end<\/span>\r\n        <span style=\"color: #0000FF\">end<\/span>\r\n    <span style=\"color: #0000FF\">end<\/span>            \r\n<span style=\"color: #0000FF\">end<\/span>\r\n<\/pre><p>Calling it:<\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">authenticateApp<\/pre><p><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/pick\/files\/badpassword.png\"> <\/p>\r\n   <p><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/pick\/files\/goodpassword.png\"> <\/p>\r\n   <p><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/pick\/files\/dogsimshow.png\"> <\/p>\r\n   <h3>Compiling<a name=\"4\"><\/a><\/h3>\r\n   <p>Now if you want to compile this, make the authentication wrapper the main function and your app will have this protection\r\n      around it.\r\n   <\/p>\r\n   <p><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/pick\/files\/getauthenticationcompiler.png\"> <\/p>\r\n   <h3>Suggestion<a name=\"5\"><\/a><\/h3>\r\n   <p>My only suggestion would be for <tt>GetAuthentication<\/tt> to pass back a third output argument about whether the user hit the cancel button.  Right now, if cancel is hit, it just\r\n      returns empty for password and username.  This is ambiguous to hitting okay with empty password and username which is a challenge\r\n      to handle if you want to use it in a loop like I did.\r\n   <\/p>\r\n   <h3>Comments<a name=\"6\"><\/a><\/h3>\r\n   <p>Give it a try and let us know what you think <a href=\"https:\/\/blogs.mathworks.com\/pick\/?p=7432#respond\">here<\/a> or leave a <a href=\"https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/23045-getauthentication#comments\">comment<\/a> for Stefan.\r\n   <\/p><script language=\"JavaScript\">\r\n<!--\r\n\r\n    function grabCode_5f224ecc798444dbbfd9c95333016167() {\r\n        \/\/ Remember the title so we can use it in the new page\r\n        title = document.title;\r\n\r\n        \/\/ Break up these strings so that their presence\r\n        \/\/ in the Javascript doesn't mess up the search for\r\n        \/\/ the MATLAB code.\r\n        t1='5f224ecc798444dbbfd9c95333016167 ' + '##### ' + 'SOURCE BEGIN' + ' #####';\r\n        t2='##### ' + 'SOURCE END' + ' #####' + ' 5f224ecc798444dbbfd9c95333016167';\r\n    \r\n        b=document.getElementsByTagName('body')[0];\r\n        i1=b.innerHTML.indexOf(t1)+t1.length;\r\n        i2=b.innerHTML.indexOf(t2);\r\n \r\n        code_string = b.innerHTML.substring(i1, i2);\r\n        code_string = code_string.replace(\/REPLACE_WITH_DASH_DASH\/g,'--');\r\n\r\n        \/\/ Use \/x3C\/g instead of the less-than character to avoid errors \r\n        \/\/ in the XML parser.\r\n        \/\/ Use '\\x26#60;' instead of '<' so that the XML parser\r\n        \/\/ doesn't go ahead and substitute the less-than character. \r\n        code_string = code_string.replace(\/\\x3C\/g, '\\x26#60;');\r\n\r\n        author = 'Sean de Wolski';\r\n        copyright = 'Copyright 2016 The MathWorks, Inc.';\r\n\r\n        w = window.open();\r\n        d = w.document;\r\n        d.write('<pre>\\n');\r\n        d.write(code_string);\r\n\r\n        \/\/ Add author and copyright lines at the bottom if specified.\r\n        if ((author.length > 0) || (copyright.length > 0)) {\r\n            d.writeln('');\r\n            d.writeln('%%');\r\n            if (author.length > 0) {\r\n                d.writeln('% _' + author + '_');\r\n            }\r\n            if (copyright.length > 0) {\r\n                d.writeln('% _' + copyright + '_');\r\n            }\r\n        }\r\n\r\n        d.write('<\/pre>\\n');\r\n      \r\n      d.title = title + ' (MATLAB code)';\r\n      d.close();\r\n      }   \r\n      \r\n-->\r\n<\/script><p style=\"text-align: right; font-size: xx-small; font-weight:lighter;   font-style: italic; color: gray\"><br><a href=\"javascript:grabCode_5f224ecc798444dbbfd9c95333016167()\"><span style=\"font-size: x-small;        font-style: italic;\">Get \r\n            the MATLAB code \r\n            <noscript>(requires JavaScript)<\/noscript><\/span><\/a><br><br>\r\n      Published with MATLAB&reg; R2016b<br><\/p>\r\n<\/div>\r\n<!--\r\n5f224ecc798444dbbfd9c95333016167 ##### SOURCE BEGIN #####\r\n%% Get Authentication\r\n%\r\n% <https:\/\/www.mathworks.com\/matlabcentral\/profile\/authors\/3208495 Sean>'s\r\n% pick this week is\r\n% <https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/23045-getauthentication GetAuthentication> by\r\n% <https:\/\/www.mathworks.com\/matlabcentral\/profile\/authors\/559000 Stefan>.\r\n% \r\n\r\n%% \r\n%\r\n% I was recently asked about a simple way to password protect an executable\r\n% compiled with <https:\/\/www.mathworks.com\/products\/compiler\/ MATLAB\r\n% Compiler>.  The customer's goal was that this would be on a shared\r\n% machine and only authorized users would be able to use the executable.\r\n%\r\n% It occurred to me that I frequently use a MATLAB-based password box every\r\n% time I upload a file to our FTP or SFTP site to share with customers.\r\n% Digging into the code there, I discovered this file and was excited to\r\n% see it publicly available on the File Exchange.\r\n%\r\n% To use it to protect your executable, simply call it in a wrapper\r\n% function to decide whether to open the main executable or quit.  Here's a\r\n% simple example that gives access to my four dogs to see a picture of\r\n% themselves.\r\n%\r\n% <include>authenticateApp.m<\/include>\r\n\r\n%%\r\n% Calling it:\r\n\r\nauthenticateApp\r\n\r\n%%\r\n%\r\n% <<badpassword.png>>\r\n%\r\n% <<goodpassword.png>>\r\n%\r\n% <<dogsimshow.png>>\r\n\r\n\r\n%% Compiling\r\n% Now if you want to compile this, make the authentication wrapper the main\r\n% function and your app will have this protection around it.\r\n%\r\n% <<getauthenticationcompiler.png>>\r\n\r\n%% Suggestion\r\n% My only suggestion would be for |GetAuthentication| to pass back a third\r\n% output argument about whether the user hit the cancel button.  Right now,\r\n% if cancel is hit, it just returns empty for password and username.  This\r\n% is ambiguous to hitting okay with empty password and username which is a\r\n% challenge to handle if you want to use it in a loop like I did.\r\n\r\n%% Comments\r\n% \r\n% Give it a try and let us know what you think\r\n% <https:\/\/blogs.mathworks.com\/pick\/?p=7432#respond here> or leave a\r\n% <https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/23045-getauthentication#comments\r\n% comment> for Stefan.\r\n%\r\n \r\n\r\n##### SOURCE END ##### 5f224ecc798444dbbfd9c95333016167\r\n-->","protected":false},"excerpt":{"rendered":"<div class=\"overview-image\"><img decoding=\"async\"  class=\"img-responsive\" src=\"https:\/\/blogs.mathworks.com\/pick\/files\/badpassword.png\" onError=\"this.style.display ='none';\" \/><\/div><p>\r\n   \r\n      Sean's pick this week is GetAuthentication by Stefan.\r\n      \r\n   \r\n   I was recently asked about a simple way to password protect an executable compiled with MATLAB Compiler.  The... <a class=\"read-more\" href=\"https:\/\/blogs.mathworks.com\/pick\/2016\/06\/24\/get-authentication\/\">read more >><\/a><\/p>","protected":false},"author":87,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[16],"tags":[],"_links":{"self":[{"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/posts\/7432"}],"collection":[{"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/users\/87"}],"replies":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/comments?post=7432"}],"version-history":[{"count":3,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/posts\/7432\/revisions"}],"predecessor-version":[{"id":7439,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/posts\/7432\/revisions\/7439"}],"wp:attachment":[{"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/media?parent=7432"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/categories?post=7432"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/tags?post=7432"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}