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