1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. /*!
  2. * Copyright (c) 2015-2016, Okta, Inc. and/or its affiliates. All rights reserved.
  3. * The Okta software accompanied by this notice is provided pursuant to the Apache License, Version 2.0 (the "License.")
  4. *
  5. * You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0.
  6. * Unless required by applicable law or agreed to in writing, software
  7. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  8. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  9. *
  10. * See the License for the specific language governing permissions and limitations under the License.
  11. */
  12. var util = require('./util');
  13. function verifyToken(idToken, key) {
  14. key = util.clone(key);
  15. var format = 'jwk';
  16. var algo = {
  17. name: 'RSASSA-PKCS1-v1_5',
  18. hash: { name: 'SHA-256' }
  19. };
  20. var extractable = true;
  21. var usages = ['verify'];
  22. // https://connect.microsoft.com/IE/feedback/details/2242108/webcryptoapi-importing-jwk-with-use-field-fails
  23. // This is a metadata tag that specifies the intent of how the key should be used.
  24. // It's not necessary to properly verify the jwt's signature.
  25. delete key.use;
  26. return crypto.subtle.importKey(
  27. format,
  28. key,
  29. algo,
  30. extractable,
  31. usages
  32. )
  33. .then(function(cryptoKey) {
  34. var jwt = idToken.split('.');
  35. var payload = util.stringToBuffer(jwt[0] + '.' + jwt[1]);
  36. var b64Signature = util.base64UrlDecode(jwt[2]);
  37. var signature = util.stringToBuffer(b64Signature);
  38. return crypto.subtle.verify(
  39. algo,
  40. cryptoKey,
  41. signature,
  42. payload
  43. );
  44. });
  45. }
  46. module.exports = {
  47. verifyToken: verifyToken
  48. };