• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
25 #include "curl_setup.h"
26 
27 #include <curl/curl.h>
28 #include "urldata.h"
29 
30 #include "vauth/vauth.h"
31 #include "curl_base64.h"
32 #include "warnless.h"
33 #include "curl_printf.h"
34 
35 /* The last #include files should be: */
36 #include "curl_memory.h"
37 #include "memdebug.h"
38 
39 /*
40  * Curl_auth_create_oauth_bearer_message()
41  *
42  * This is used to generate an already encoded OAuth 2.0 message ready for
43  * sending to the recipient.
44  *
45  * Parameters:
46  *
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.
55  *
56  * Returns CURLE_OK on success.
57  */
Curl_auth_create_oauth_bearer_message(struct Curl_easy * data,const char * user,const char * host,const long port,const char * bearer,char ** outptr,size_t * outlen)58 CURLcode Curl_auth_create_oauth_bearer_message(struct Curl_easy *data,
59                                                const char *user,
60                                                const char *host,
61                                                const long port,
62                                                const char *bearer,
63                                                char **outptr, size_t *outlen)
64 {
65   CURLcode result = CURLE_OK;
66   char *oauth = NULL;
67 
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,
73                     bearer);
74   else
75     oauth = aprintf("user=%s\1host=%s\1port=%ld\1auth=Bearer %s\1\1", user,
76                     host, port, bearer);
77   if(!oauth)
78     return CURLE_OUT_OF_MEMORY;
79 
80   /* Base64 encode the reply */
81   result = Curl_base64_encode(data, oauth, strlen(oauth), outptr, outlen);
82 
83   free(oauth);
84 
85   return result;
86 }
87