1 /***************************************************************************
3 * Project ___| | | | _ \| |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
8 * Copyright (C) 1998 - 2016, Daniel Stenberg, <daniel@haxx.se>, et al.
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.
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.
18 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
19 * KIND, either express or implied.
21 * RFC6749 OAuth 2.0 Authorization Framework
23 ***************************************************************************/
25 #include "curl_setup.h"
27 #include <curl/curl.h>
30 #include "vauth/vauth.h"
31 #include "curl_base64.h"
33 #include "curl_printf.h"
35 /* The last #include files should be: */
36 #include "curl_memory.h"
40 * Curl_auth_create_oauth_bearer_message()
42 * This is used to generate an already encoded OAuth 2.0 message ready for
43 * sending to the recipient.
47 * data[in] - The session handle.
48 * user[in] - The user name.
49 * host[in] - The host name(for OAUTHBEARER).
50 * port[in] - The port(for OAUTHBEARER when not Port 80).
51 * bearer[in] - The bearer token.
52 * outptr[in / out] - The address where a pointer to newly allocated memory
53 * holding the result will be stored upon completion.
54 * outlen[out] - The length of the output message.
56 * Returns CURLE_OK on success.
58 CURLcode Curl_auth_create_oauth_bearer_message(struct Curl_easy *data,
63 char **outptr, size_t *outlen)
65 CURLcode result = CURLE_OK;
68 /* Generate the message */
69 if(host == NULL && (port == 0 || port == 80))
70 oauth = aprintf("user=%s\1auth=Bearer %s\1\1", user, bearer);
71 else if(port == 0 || port == 80)
72 oauth = aprintf("user=%s\1host=%s\1auth=Bearer %s\1\1", user, host,
75 oauth = aprintf("user=%s\1host=%s\1port=%ld\1auth=Bearer %s\1\1", user,
78 return CURLE_OUT_OF_MEMORY;
80 /* Base64 encode the reply */
81 result = Curl_base64_encode(data, oauth, strlen(oauth), outptr, outlen);