Coverage Report

Created: 2026-05-30 06:06

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/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/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(struct Curl_creds *creds,
51
                                               const char *host,
52
                                               const long port,
53
                                               struct bufref *out)
54
0
{
55
0
  char *oauth;
56
57
  /* Generate the message */
58
0
  if(port == 0 || port == 80)
59
0
    oauth = curl_maprintf("n,a=%s,\1host=%s\1auth=Bearer %s\1\1",
60
0
                          Curl_creds_user(creds), host,
61
0
                          Curl_creds_oauth_bearer(creds));
62
0
  else
63
0
    oauth = curl_maprintf("n,a=%s,\1host=%s\1port=%ld\1auth=Bearer %s\1\1",
64
0
                          Curl_creds_user(creds), host, port,
65
0
                          Curl_creds_oauth_bearer(creds));
66
0
  if(!oauth)
67
0
    return CURLE_OUT_OF_MEMORY;
68
69
0
  Curl_bufref_set(out, oauth, strlen(oauth), curl_free);
70
0
  return CURLE_OK;
71
0
}
72
73
/*
74
 * Curl_auth_create_xoauth_bearer_message()
75
 *
76
 * This is used to generate a XOAuth 2.0 message ready for * sending to the
77
 * recipient.
78
 *
79
 * Parameters:
80
 *
81
 * user[in]         - The username.
82
 * bearer[in]       - The bearer token.
83
 * out[out]         - The result storage.
84
 *
85
 * Returns CURLE_OK on success.
86
 */
87
CURLcode Curl_auth_create_xoauth_bearer_message(struct Curl_creds *creds,
88
                                                struct bufref *out)
89
0
{
90
  /* Generate the message */
91
0
  char *xoauth = curl_maprintf("user=%s\1auth=Bearer %s\1\1",
92
0
                               Curl_creds_user(creds),
93
0
                               Curl_creds_oauth_bearer(creds));
94
0
  if(!xoauth)
95
0
    return CURLE_OUT_OF_MEMORY;
96
97
0
  Curl_bufref_set(out, xoauth, strlen(xoauth), curl_free);
98
0
  return CURLE_OK;
99
0
}
100
#endif /* disabled, no users */