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