Coverage Report

Created: 2025-10-10 06:31

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/PROJ/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
#include "../urldata.h"
35
36
#include "vauth.h"
37
#include "../curlx/warnless.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 username.
52
 * host[in]         - The hostname.
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
0
{
65
0
  char *oauth;
66
67
  /* Generate the message */
68
0
  if(port == 0 || port == 80)
69
0
    oauth = curl_maprintf("n,a=%s,\1host=%s\1auth=Bearer %s\1\1", user, host,
70
0
                          bearer);
71
0
  else
72
0
    oauth = curl_maprintf("n,a=%s,\1host=%s\1port=%ld\1auth=Bearer %s\1\1",
73
0
                          user, host, port, bearer);
74
0
  if(!oauth)
75
0
    return CURLE_OUT_OF_MEMORY;
76
77
0
  Curl_bufref_set(out, oauth, strlen(oauth), curl_free);
78
0
  return CURLE_OK;
79
0
}
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 username.
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
0
{
99
  /* Generate the message */
100
0
  char *xoauth = curl_maprintf("user=%s\1auth=Bearer %s\1\1", user, bearer);
101
0
  if(!xoauth)
102
0
    return CURLE_OUT_OF_MEMORY;
103
104
0
  Curl_bufref_set(out, xoauth, strlen(xoauth), curl_free);
105
0
  return CURLE_OK;
106
0
}
107
#endif /* disabled, no users */