oauth2.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 1998 - 2022, 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.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. * SPDX-License-Identifier: curl
  22. *
  23. * RFC6749 OAuth 2.0 Authorization Framework
  24. *
  25. ***************************************************************************/
  26. #include "curl_setup.h"
  27. #if !defined(CURL_DISABLE_IMAP) || !defined(CURL_DISABLE_SMTP) || \
  28. !defined(CURL_DISABLE_POP3)
  29. #include <curl/curl.h>
  30. #include "urldata.h"
  31. #include "vauth/vauth.h"
  32. #include "warnless.h"
  33. #include "curl_printf.h"
  34. /* The last #include files should be: */
  35. #include "curl_memory.h"
  36. #include "memdebug.h"
  37. /*
  38. * Curl_auth_create_oauth_bearer_message()
  39. *
  40. * This is used to generate an OAuth 2.0 message ready for sending to the
  41. * recipient.
  42. *
  43. * Parameters:
  44. *
  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. * out[out] - The result storage.
  50. *
  51. * Returns CURLE_OK on success.
  52. */
  53. CURLcode Curl_auth_create_oauth_bearer_message(const char *user,
  54. const char *host,
  55. const long port,
  56. const char *bearer,
  57. struct bufref *out)
  58. {
  59. char *oauth;
  60. /* Generate the message */
  61. if(port == 0 || port == 80)
  62. oauth = aprintf("n,a=%s,\1host=%s\1auth=Bearer %s\1\1", user, host,
  63. bearer);
  64. else
  65. oauth = aprintf("n,a=%s,\1host=%s\1port=%ld\1auth=Bearer %s\1\1", user,
  66. host, port, bearer);
  67. if(!oauth)
  68. return CURLE_OUT_OF_MEMORY;
  69. Curl_bufref_set(out, oauth, strlen(oauth), curl_free);
  70. return CURLE_OK;
  71. }
  72. /*
  73. * Curl_auth_create_xoauth_bearer_message()
  74. *
  75. * This is used to generate a XOAuth 2.0 message ready for * sending to the
  76. * recipient.
  77. *
  78. * Parameters:
  79. *
  80. * user[in] - The user name.
  81. * bearer[in] - The bearer token.
  82. * out[out] - The result storage.
  83. *
  84. * Returns CURLE_OK on success.
  85. */
  86. CURLcode Curl_auth_create_xoauth_bearer_message(const char *user,
  87. const char *bearer,
  88. struct bufref *out)
  89. {
  90. /* Generate the message */
  91. char *xoauth = aprintf("user=%s\1auth=Bearer %s\1\1", user, bearer);
  92. if(!xoauth)
  93. return CURLE_OUT_OF_MEMORY;
  94. Curl_bufref_set(out, xoauth, strlen(xoauth), curl_free);
  95. return CURLE_OK;
  96. }
  97. #endif /* disabled, no users */