oauth2.c 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 1998 - 2016, 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. #include <curl/curl.h>
  26. #include "urldata.h"
  27. #include "vauth/vauth.h"
  28. #include "curl_base64.h"
  29. #include "warnless.h"
  30. #include "curl_printf.h"
  31. /* The last #include files should be: */
  32. #include "curl_memory.h"
  33. #include "memdebug.h"
  34. /*
  35. * Curl_auth_create_oauth_bearer_message()
  36. *
  37. * This is used to generate an already encoded OAuth 2.0 message ready for
  38. * sending to the recipient.
  39. *
  40. * Parameters:
  41. *
  42. * data[in] - The session handle.
  43. * user[in] - The user name.
  44. * host[in] - The host name(for OAUTHBEARER).
  45. * port[in] - The port(for OAUTHBEARER when not Port 80).
  46. * bearer[in] - The bearer token.
  47. * outptr[in / out] - The address where a pointer to newly allocated memory
  48. * holding the result will be stored upon completion.
  49. * outlen[out] - The length of the output message.
  50. *
  51. * Returns CURLE_OK on success.
  52. */
  53. CURLcode Curl_auth_create_oauth_bearer_message(struct Curl_easy *data,
  54. const char *user,
  55. const char *host,
  56. const long port,
  57. const char *bearer,
  58. char **outptr, size_t *outlen)
  59. {
  60. CURLcode result = CURLE_OK;
  61. char *oauth = NULL;
  62. /* Generate the message */
  63. if(host == NULL && (port == 0 || port == 80))
  64. oauth = aprintf("user=%s\1auth=Bearer %s\1\1", user, bearer);
  65. else if(port == 0 || port == 80)
  66. oauth = aprintf("user=%s\1host=%s\1auth=Bearer %s\1\1", user, host,
  67. bearer);
  68. else
  69. oauth = aprintf("user=%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. }