ed61ee8e08aed1cc04fb157774379fb6321d6fed
[collectd.git] / src / utils_oauth.h
1 /**
2  * collectd - src/utils_oauth.h
3  * ISC license
4  *
5  * Copyright (C) 2017  Florian Forster
6  *
7  * Permission to use, copy, modify, and/or distribute this software for any
8  * purpose with or without fee is hereby granted, provided that the above
9  * copyright notice and this permission notice appear in all copies.
10  *
11  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
12  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
14  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
17  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18  *
19  * Authors:
20  *   Florian Forster <octo at collectd.org>
21  **/
22
23 #ifndef UTILS_OAUTH_H
24 #define UTILS_OAUTH_H
25
26 #include "collectd.h"
27 #include "utils_time.h"
28
29 #ifndef GOOGLE_OAUTH_URL
30 #define GOOGLE_OAUTH_URL "https://www.googleapis.com/oauth2/v3/token"
31 #endif
32
33 #include <openssl/evp.h>
34
35 struct oauth_s;
36 typedef struct oauth_s oauth_t;
37
38 int oauth_parse_json_token(char const *json, char *out_access_token,
39                            size_t access_token_size, cdtime_t *expires_in);
40
41 oauth_t *oauth_create(char const *url, char const *iss, char const *scope,
42                       char const *aud, EVP_PKEY *key);
43 oauth_t *oauth_create_p12(char const *url, char const *iss, char const *scope,
44                           char const *aud, char const *file, char const *pass);
45
46 typedef struct {
47   char *project_id;
48   oauth_t *oauth;
49 } oauth_google_t;
50
51 /* oauth_create_google_json creates an OAuth object from JSON encoded
52  * credentials. */
53 oauth_google_t oauth_create_google_json(char const *json, char const *scope);
54
55 /* oauth_create_google_file reads path, which contains JSON encoded service
56  * account credentials, and returns an OAuth object. */
57 oauth_google_t oauth_create_google_file(char const *path, char const *scope);
58
59 /* oauth_create_google_default looks for service account credentials in a couple
60  * of well-known places and returns an OAuth object if found. The well known
61  * locations are:
62  *
63  *   - ${GOOGLE_APPLICATION_CREDENTIALS}
64  *   - ${HOME}/.config/gcloud/application_default_credentials.json
65  */
66 oauth_google_t oauth_create_google_default(char const *scope);
67
68 /* oauth_destroy frees all resources associated with an OAuth object. */
69 void oauth_destroy(oauth_t *auth);
70
71 int oauth_access_token(oauth_t *auth, char *buffer, size_t buffer_size);
72
73 #endif