109 lines
3.3 KiB
C
Raw Normal View History

2016-09-11 17:22:32 +02:00
/***************************************************************************
* _ _ ____ _
* Project ___| | | | _ \| |
* / __| | | | |_) | |
* | (__| |_| | _ <| |___
* \___|\___/|_| \_\_____|
*
2023-07-02 19:51:09 +02:00
* Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al.
2016-09-11 17:22:32 +02:00
*
* This software is licensed as described in the file COPYING, which
* you should have received as part of this distribution. The terms
2021-09-14 00:13:48 +02:00
* are also available at https://curl.se/docs/copyright.html.
2016-09-11 17:22:32 +02:00
*
* You may opt to use, copy, modify, merge, publish, distribute and/or sell
* copies of the Software, and permit persons to whom the Software is
* furnished to do so, under the terms of the COPYING file.
*
* This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
* KIND, either express or implied.
*
2022-11-16 20:14:03 +01:00
* SPDX-License-Identifier: curl
*
2016-09-11 17:22:32 +02:00
* RFC6749 OAuth 2.0 Authorization Framework
*
***************************************************************************/
#include "curl_setup.h"
2019-11-11 23:01:05 +01:00
#if !defined(CURL_DISABLE_IMAP) || !defined(CURL_DISABLE_SMTP) || \
2023-07-02 19:51:09 +02:00
!defined(CURL_DISABLE_POP3) || \
(!defined(CURL_DISABLE_LDAP) && defined(USE_OPENLDAP))
2019-11-11 23:01:05 +01:00
2016-09-11 17:22:32 +02:00
#include <curl/curl.h>
#include "urldata.h"
#include "vauth/vauth.h"
#include "warnless.h"
#include "curl_printf.h"
/* The last #include files should be: */
#include "curl_memory.h"
#include "memdebug.h"
/*
* Curl_auth_create_oauth_bearer_message()
*
2021-09-14 00:13:48 +02:00
* This is used to generate an OAuth 2.0 message ready for sending to the
* recipient.
2016-09-11 17:22:32 +02:00
*
* Parameters:
*
* user[in] - The user name.
2019-11-11 23:01:05 +01:00
* host[in] - The host name.
* port[in] - The port(when not Port 80).
2016-09-11 17:22:32 +02:00
* bearer[in] - The bearer token.
2021-09-14 00:13:48 +02:00
* out[out] - The result storage.
2016-09-11 17:22:32 +02:00
*
* Returns CURLE_OK on success.
*/
2021-09-14 00:13:48 +02:00
CURLcode Curl_auth_create_oauth_bearer_message(const char *user,
2016-09-11 17:22:32 +02:00
const char *host,
const long port,
const char *bearer,
2021-09-14 00:13:48 +02:00
struct bufref *out)
2016-09-11 17:22:32 +02:00
{
2021-09-14 00:13:48 +02:00
char *oauth;
2016-09-11 17:22:32 +02:00
/* Generate the message */
2019-11-11 23:01:05 +01:00
if(port == 0 || port == 80)
oauth = aprintf("n,a=%s,\1host=%s\1auth=Bearer %s\1\1", user, host,
2016-09-11 17:22:32 +02:00
bearer);
else
2019-11-11 23:01:05 +01:00
oauth = aprintf("n,a=%s,\1host=%s\1port=%ld\1auth=Bearer %s\1\1", user,
2016-09-11 17:22:32 +02:00
host, port, bearer);
if(!oauth)
return CURLE_OUT_OF_MEMORY;
2021-09-14 00:13:48 +02:00
Curl_bufref_set(out, oauth, strlen(oauth), curl_free);
return CURLE_OK;
2016-09-11 17:22:32 +02:00
}
2019-11-11 23:01:05 +01:00
/*
* Curl_auth_create_xoauth_bearer_message()
*
2021-09-14 00:13:48 +02:00
* This is used to generate a XOAuth 2.0 message ready for * sending to the
* recipient.
2019-11-11 23:01:05 +01:00
*
* Parameters:
*
* user[in] - The user name.
* bearer[in] - The bearer token.
2021-09-14 00:13:48 +02:00
* out[out] - The result storage.
2019-11-11 23:01:05 +01:00
*
* Returns CURLE_OK on success.
*/
2021-09-14 00:13:48 +02:00
CURLcode Curl_auth_create_xoauth_bearer_message(const char *user,
2019-11-11 23:01:05 +01:00
const char *bearer,
2021-09-14 00:13:48 +02:00
struct bufref *out)
2019-11-11 23:01:05 +01:00
{
/* Generate the message */
char *xoauth = aprintf("user=%s\1auth=Bearer %s\1\1", user, bearer);
if(!xoauth)
return CURLE_OUT_OF_MEMORY;
2021-09-14 00:13:48 +02:00
Curl_bufref_set(out, xoauth, strlen(xoauth), curl_free);
return CURLE_OK;
2019-11-11 23:01:05 +01:00
}
#endif /* disabled, no users */