/src/curl/lib/vauth/oauth2.c
Line | Count | Source |
1 | | /*************************************************************************** |
2 | | * _ _ ____ _ |
3 | | * Project ___| | | | _ \| | |
4 | | * / __| | | | |_) | | |
5 | | * | (__| |_| | _ <| |___ |
6 | | * \___|\___/|_| \_\_____| |
7 | | * |
8 | | * Copyright (C) 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 | | * SPDX-License-Identifier: curl |
22 | | * |
23 | | * RFC6749 OAuth 2.0 Authorization Framework |
24 | | * |
25 | | ***************************************************************************/ |
26 | | #include "../curl_setup.h" |
27 | | |
28 | | #if !defined(CURL_DISABLE_IMAP) || !defined(CURL_DISABLE_SMTP) || \ |
29 | | !defined(CURL_DISABLE_POP3) || \ |
30 | | (!defined(CURL_DISABLE_LDAP) && defined(USE_OPENLDAP)) |
31 | | |
32 | | #include "vauth.h" |
33 | | |
34 | | /* |
35 | | * Curl_auth_create_oauth_bearer_message() |
36 | | * |
37 | | * This is used to generate an OAuth 2.0 message ready for sending to the |
38 | | * recipient. |
39 | | * |
40 | | * Parameters: |
41 | | * |
42 | | * user[in] - The username. |
43 | | * host[in] - The hostname. |
44 | | * port[in] - The port(when not Port 80). |
45 | | * bearer[in] - The bearer token. |
46 | | * out[out] - The result storage. |
47 | | * |
48 | | * Returns CURLE_OK on success. |
49 | | */ |
50 | | CURLcode Curl_auth_create_oauth_bearer_message(const char *user, |
51 | | const char *host, |
52 | | const long port, |
53 | | const char *bearer, |
54 | | struct bufref *out) |
55 | 0 | { |
56 | 0 | char *oauth; |
57 | | |
58 | | /* Generate the message */ |
59 | 0 | if(port == 0 || port == 80) |
60 | 0 | oauth = curl_maprintf("n,a=%s,\1host=%s\1auth=Bearer %s\1\1", user, host, |
61 | 0 | bearer); |
62 | 0 | else |
63 | 0 | oauth = curl_maprintf("n,a=%s,\1host=%s\1port=%ld\1auth=Bearer %s\1\1", |
64 | 0 | user, host, port, bearer); |
65 | 0 | if(!oauth) |
66 | 0 | return CURLE_OUT_OF_MEMORY; |
67 | | |
68 | 0 | Curl_bufref_set(out, oauth, strlen(oauth), curl_free); |
69 | 0 | return CURLE_OK; |
70 | 0 | } |
71 | | |
72 | | /* |
73 | | * Curl_auth_create_xoauth_bearer_message() |
74 | | * |
75 | | * This is used to generate a XOAuth 2.0 message ready for * sending to the |
76 | | * recipient. |
77 | | * |
78 | | * Parameters: |
79 | | * |
80 | | * user[in] - The username. |
81 | | * bearer[in] - The bearer token. |
82 | | * out[out] - The result storage. |
83 | | * |
84 | | * Returns CURLE_OK on success. |
85 | | */ |
86 | | CURLcode Curl_auth_create_xoauth_bearer_message(const char *user, |
87 | | const char *bearer, |
88 | | struct bufref *out) |
89 | 0 | { |
90 | | /* Generate the message */ |
91 | 0 | char *xoauth = curl_maprintf("user=%s\1auth=Bearer %s\1\1", user, bearer); |
92 | 0 | if(!xoauth) |
93 | 0 | return CURLE_OUT_OF_MEMORY; |
94 | | |
95 | 0 | Curl_bufref_set(out, xoauth, strlen(xoauth), curl_free); |
96 | 0 | return CURLE_OK; |
97 | 0 | } |
98 | | #endif /* disabled, no users */ |