oauth2.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 1998 - 2019, Daniel Stenberg, <daniel@haxx.se>, et al.
  9. *
  10. * This software is licensed as described in the file COPYING, which
  11. * you should have received as part of this distribution. The terms
  12. * are also available at https://curl.haxx.se/docs/copyright.html.
  13. *
  14. * You may opt to use, copy, modify, merge, publish, distribute and/or sell
  15. * copies of the Software, and permit persons to whom the Software is
  16. * furnished to do so, under the terms of the COPYING file.
  17. *
  18. * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
  19. * KIND, either express or implied.
  20. *
  21. * RFC6749 OAuth 2.0 Authorization Framework
  22. *
  23. ***************************************************************************/
  24. #include "curl_setup.h"
  25. #if !defined(CURL_DISABLE_IMAP) || !defined(CURL_DISABLE_SMTP) || \
  26. !defined(CURL_DISABLE_POP3)
  27. #include <curl/curl.h>
  28. #include "urldata.h"
  29. #include "vauth/vauth.h"
  30. #include "curl_base64.h"
  31. #include "warnless.h"
  32. #include "curl_printf.h"
  33. /* The last #include files should be: */
  34. #include "curl_memory.h"
  35. #include "memdebug.h"
  36. /*
  37. * Curl_auth_create_oauth_bearer_message()
  38. *
  39. * This is used to generate an already encoded OAuth 2.0 message ready for
  40. * sending to the recipient.
  41. *
  42. * Parameters:
  43. *
  44. * data[in] - The session handle.
  45. * user[in] - The user name.
  46. * host[in] - The host name.
  47. * port[in] - The port(when not Port 80).
  48. * bearer[in] - The bearer token.
  49. * outptr[in / out] - The address where a pointer to newly allocated memory
  50. * holding the result will be stored upon completion.
  51. * outlen[out] - The length of the output message.
  52. *
  53. * Returns CURLE_OK on success.
  54. */
  55. CURLcode Curl_auth_create_oauth_bearer_message(struct Curl_easy *data,
  56. const char *user,
  57. const char *host,
  58. const long port,
  59. const char *bearer,
  60. char **outptr, size_t *outlen)
  61. {
  62. CURLcode result = CURLE_OK;
  63. char *oauth = NULL;
  64. /* Generate the message */
  65. if(port == 0 || port == 80)
  66. oauth = aprintf("n,a=%s,\1host=%s\1auth=Bearer %s\1\1", user, host,
  67. bearer);
  68. else
  69. oauth = aprintf("n,a=%s,\1host=%s\1port=%ld\1auth=Bearer %s\1\1", user,
  70. host, port, bearer);
  71. if(!oauth)
  72. return CURLE_OUT_OF_MEMORY;
  73. /* Base64 encode the reply */
  74. result = Curl_base64_encode(data, oauth, strlen(oauth), outptr, outlen);
  75. free(oauth);
  76. return result;
  77. }
  78. /*
  79. * Curl_auth_create_xoauth_bearer_message()
  80. *
  81. * This is used to generate an already encoded XOAuth 2.0 message ready for
  82. * sending to the recipient.
  83. *
  84. * Parameters:
  85. *
  86. * data[in] - The session handle.
  87. * user[in] - The user name.
  88. * bearer[in] - The bearer token.
  89. * outptr[in / out] - The address where a pointer to newly allocated memory
  90. * holding the result will be stored upon completion.
  91. * outlen[out] - The length of the output message.
  92. *
  93. * Returns CURLE_OK on success.
  94. */
  95. CURLcode Curl_auth_create_xoauth_bearer_message(struct Curl_easy *data,
  96. const char *user,
  97. const char *bearer,
  98. char **outptr, size_t *outlen)
  99. {
  100. CURLcode result = CURLE_OK;
  101. /* Generate the message */
  102. char *xoauth = aprintf("user=%s\1auth=Bearer %s\1\1", user, bearer);
  103. if(!xoauth)
  104. return CURLE_OUT_OF_MEMORY;
  105. /* Base64 encode the reply */
  106. result = Curl_base64_encode(data, xoauth, strlen(xoauth), outptr, outlen);
  107. free(xoauth);
  108. return result;
  109. }
  110. #endif /* disabled, no users */