2
0

oauth2.c 3.3 KB

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