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