Guest User

Untitled

a guest
Jan 22nd, 2018
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.92 KB | None | 0 0
  1. component persistent="false" accessors="true" output="false" {
  2.  
  3. public any function authenticateReuqest(required string verb,required string cfc,required struct requestArguments,required struct requestHeaders) {
  4.  
  5. // Check for Authorisation headers
  6. if(not structkeyexists(arguments.requestHeaders,"Authorization")) {
  7. return createAuthenticationRequiredMessage("Authentication Required");
  8. }
  9.  
  10. // Check Authorization valid
  11. local.apiAccess=retrieveApiUserFromAuthorizationHeader(arguments.requestHeaders["Authorization"]);
  12.  
  13. if(not len(local.apiAccess)) {
  14. return createAuthenticationRequiredMessage("Invalid login credentials provided");
  15. }
  16. if(local.apiAccess eq true) {
  17. return true;
  18. }
  19. return createAuthenticationRequiredMessage("Invalid login credentials provided");
  20. }
  21.  
  22. public any function createAuthenticationRequiredMessage(string message) {
  23. local.bodyContent=structnew();
  24. local.returnHeaders=structnew();
  25. local.reponseObject=createObject("component","taffy.core.genericRepresentation");
  26. bodycontent.msg=arguments.message;
  27. structinsert(local.returnHeaders,"WWW-Authenticate","Basic realm=""App API - #arguments.message#""");
  28. return reponseObject.setData(local.bodyContent).withStatus(401).withHeaders(local.returnHeaders);
  29. }
  30.  
  31. public any function retrieveApiUserFromAuthorizationHeader(required string authorizationHeader) {
  32. local.decodedAuthHeader=tostring(tobinary(listlast(arguments.authorizationHeader," ")));
  33. local.username=ListFirst(local.decodedAuthHeader,":");
  34. local.password=Listlast(local.decodedAuthHeader,":");
  35. return validateLoginCredentials(local.username,local.password);
  36. }
  37.  
  38. public any function validateLoginCredentials(required string login,required string password) {
  39. local.result=getDAO().readByUserNameandPassword(arguments.login,arguments.password);
  40. // If we have a match return true
  41. if(!isNull(local.result)) {
  42. return true;
  43. }
  44. // Default is always false.
  45. return False;
  46. }
  47.  
  48. }
Add Comment
Please, Sign In to add comment