1 /***************************************************************************
2 * _ _ ____ _
3 * Project ___| | | | _ \| |
4 * / __| | | | |_) | |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
7 *
8 * Copyright (C) 1998 - 2019, 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 #if !defined(CURL_DISABLE_IMAP) || !defined(CURL_DISABLE_SMTP) || \
28 !defined(CURL_DISABLE_POP3)
29
30 #include <curl/curl.h>
31 #include "urldata.h"
32
33 #include "vauth/vauth.h"
34 #include "curl_base64.h"
35 #include "warnless.h"
36 #include "curl_printf.h"
37
38 /* The last #include files should be: */
39 #include "curl_memory.h"
40 #include "memdebug.h"
41
42 /*
43 * Curl_auth_create_oauth_bearer_message()
44 *
45 * This is used to generate an already encoded OAuth 2.0 message ready for
46 * sending to the recipient.
47 *
48 * Parameters:
49 *
50 * data[in] - The session handle.
51 * user[in] - The user name.
52 * host[in] - The host name.
53 * port[in] - The port(when not Port 80).
54 * bearer[in] - The bearer token.
55 * outptr[in / out] - The address where a pointer to newly allocated memory
56 * holding the result will be stored upon completion.
57 * outlen[out] - The length of the output message.
58 *
59 * Returns CURLE_OK on success.
60 */
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)61 CURLcode Curl_auth_create_oauth_bearer_message(struct Curl_easy *data,
62 const char *user,
63 const char *host,
64 const long port,
65 const char *bearer,
66 char **outptr, size_t *outlen)
67 {
68 CURLcode result = CURLE_OK;
69 char *oauth = NULL;
70
71 /* Generate the message */
72 if(port == 0 || port == 80)
73 oauth = aprintf("n,a=%s,\1host=%s\1auth=Bearer %s\1\1", user, host,
74 bearer);
75 else
76 oauth = aprintf("n,a=%s,\1host=%s\1port=%ld\1auth=Bearer %s\1\1", user,
77 host, port, bearer);
78 if(!oauth)
79 return CURLE_OUT_OF_MEMORY;
80
81 /* Base64 encode the reply */
82 result = Curl_base64_encode(data, oauth, strlen(oauth), outptr, outlen);
83
84 free(oauth);
85
86 return result;
87 }
88
89 /*
90 * Curl_auth_create_xoauth_bearer_message()
91 *
92 * This is used to generate an already encoded XOAuth 2.0 message ready for
93 * sending to the recipient.
94 *
95 * Parameters:
96 *
97 * data[in] - The session handle.
98 * user[in] - The user name.
99 * bearer[in] - The bearer token.
100 * outptr[in / out] - The address where a pointer to newly allocated memory
101 * holding the result will be stored upon completion.
102 * outlen[out] - The length of the output message.
103 *
104 * Returns CURLE_OK on success.
105 */
Curl_auth_create_xoauth_bearer_message(struct Curl_easy * data,const char * user,const char * bearer,char ** outptr,size_t * outlen)106 CURLcode Curl_auth_create_xoauth_bearer_message(struct Curl_easy *data,
107 const char *user,
108 const char *bearer,
109 char **outptr, size_t *outlen)
110 {
111 CURLcode result = CURLE_OK;
112
113 /* Generate the message */
114 char *xoauth = aprintf("user=%s\1auth=Bearer %s\1\1", user, bearer);
115 if(!xoauth)
116 return CURLE_OUT_OF_MEMORY;
117
118 /* Base64 encode the reply */
119 result = Curl_base64_encode(data, xoauth, strlen(xoauth), outptr, outlen);
120
121 free(xoauth);
122
123 return result;
124 }
125 #endif /* disabled, no users */
126
127