Advertisement
Guest User

Untitled

a guest
Dec 18th, 2016
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.91 KB | None | 0 0
  1. /**
  2. * Create a Token from the submitted data.<br/>
  3. *
  4. * @ApiDoc(
  5. * resource = true,
  6. * description = "Creates a new token from the submitted data.",
  7. * statusCodes = {
  8. * 200 = "Returned when successful",
  9. * 400 = "Returned when the form has errors"
  10. * }
  11. * )
  12. *
  13. * @param ParamFetcher $paramFetcher Paramfetcher
  14. *
  15. * @RequestParam(name="username", nullable=false, strict=true, description="username.")
  16. * @RequestParam(name="password", nullable=false, strict=true, description="password.")
  17. * @RequestParam(name="salt", nullable=false, strict=true, description="salt.")
  18. *
  19. * @return View
  20. */
  21. public function postTokenAction(ParamFetcher $paramFetcher)
  22. {
  23.  
  24. $view = View::create();
  25.  
  26. $userManager = $this->get('fos_user.user_manager');
  27. $user = $userManager->findUserByUsername($paramFetcher->get('username'));
  28.  
  29. if (!$user instanceof User) {
  30. $view->setStatusCode(404)->setData("Data received succesfully but with errors.");
  31.  
  32. return $view;
  33. }
  34.  
  35. $factory = $this->get('security.encoder_factory');
  36.  
  37. $encoder = $factory->getEncoder($user);
  38. $password = $encoder->encodePassword($paramFetcher->get('password'), $paramFetcher->get('salt'));
  39.  
  40. $header = $this->generateToken($paramFetcher->get('username'), $password);
  41. $data = array('X-WSSE' => $header);
  42. $view->setHeader("Authorization", 'WSSE profile="UsernameToken"');
  43. $view->setHeader("X-WSSE", $header);
  44. $view->setStatusCode(200)->setData($data);
  45.  
  46. return $view;
  47. }
  48.  
  49. /**
  50. * Generate token for username given
  51. *
  52. * @param string $username username
  53. * @param string $password password with salt included
  54. * @return string
  55. */
  56. private function generateToken($username, $password)
  57. {
  58. $created = date('c');
  59. $nonce = substr(md5(uniqid('nonce_', true)), 0, 16);
  60. $nonceSixtyFour = base64_encode($nonce);
  61. $passwordDigest = base64_encode(sha1($nonce . $created . $password, true));
  62.  
  63. $token = sprintf(
  64. 'UsernameToken Username="%s", PasswordDigest="%s", Nonce="%s", Created="%s"',
  65. $username,
  66. $passwordDigest,
  67. $nonceSixtyFour,
  68. $created
  69. );
  70.  
  71. return $token;
  72. }
  73.  
  74. .controller('SignInCtrl', function ($rootScope, $scope, $http, $cookies, Salt, Digest, envService) {
  75.  
  76. var apiUrl = envService.read('apiUrl');
  77.  
  78. $scope.username = null;
  79. $scope.password = null;
  80.  
  81. $scope.getSalt = function (username, password) {
  82. var data = {
  83. username: $scope.username,
  84. password: $scope.password
  85. };
  86. $http({
  87. method: 'GET',
  88. url: apiUrl + 'v1/users/' + username + '/salt.json',
  89. data: JSON.stringify(data),
  90. headers: {
  91. 'Content-Type': 'application/json'
  92. }
  93. }).then(function (response) {
  94. if (response.data) {
  95. var salt = response.data;
  96. $scope.salt = salt;
  97. // Encrypt password accordingly to generate secret
  98. Digest.cipher(password, salt).then(function(secret){
  99. // Display salt and secret for this example
  100. $scope.salt = salt;
  101. $scope.secret = secret;
  102. // Store auth informations in cookies for page refresh
  103. $cookies.username = $scope.username;
  104. $cookies.secret = secret;
  105. $cookies.salt = $scope.salt;
  106. // Store auth informations in rootScope for multi views access
  107. $rootScope.userAuth = {username: $scope.username, secret : $scope.secret, salt : $scope.salt };
  108. var dataToken = {
  109. username: $scope.username,
  110. password: $scope.password,
  111. salt: $scope.salt
  112. };
  113.  
  114. $http({
  115. method: 'POST',
  116. url: apiUrl + 'v1/tokens.json',
  117. dataToken: JSON.stringify(data),
  118. headers: {
  119. 'Content-Type': 'application/json'
  120. }
  121. }).then(function (response) {
  122. if (response.dataToken) {
  123. console.log("Token found");
  124. $window.sessionStorage.token = reponse.dataToken;
  125. }
  126. })
  127. }, function(err){
  128. console.log(err);
  129. });
  130. }
  131. },
  132. function (response) {
  133. delete $window.sessionStorage.token;
  134. $scope.msg = "Service not Exists";
  135. $scope.statusval = response.status;
  136. $scope.statustext = response.statusText;
  137. $scope.headers = response.headers();
  138. }
  139. );
  140. };
  141. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement