Coverage Report

Created: 2025-12-03 07:02

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