summaryrefslogtreecommitdiff
path: root/examples/gitlab-oauth2-example.c
blob: f18cbab55b81265f63df069f0f1e3060d6e4e0cb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
/* gitlab-oauth2-example.c
 *
 * Copyright 2021 Günther Wagner <info@gunibert.de>
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms and conditions of the GNU Lesser General Public License,
 * version 2.1, as published by the Free Software Foundation.
 *
 * This program is distributed in the hope it will be useful, but WITHOUT ANY
 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License for
 * more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
 */

/**
 * This example shows the recommended PKCE authorization flow for Gitlab.
 */

#include <glib.h>
#include <rest/rest.h>
#include <stdio.h>
#include <libsoup/soup.h>

GMainLoop *loop;

static void
load_projects (RestProxy *proxy)
{
  g_autoptr(GError) error = NULL;
  RestProxyCall *call;

  call = rest_proxy_new_call (proxy);
  rest_proxy_call_set_method (call, "GET");
  rest_proxy_call_set_function (call, "projects/426/issues");
  rest_proxy_call_sync (call, &error);

  g_print ("%s\n", rest_proxy_call_get_payload (call));
  g_main_loop_quit (loop);
}

static void
gitlab_oauth2_example_refreshed_access_token (GObject      *object,
                                              GAsyncResult *result,
                                              gpointer      user_data)
{
  RestOAuth2Proxy *oauth2_proxy = (RestOAuth2Proxy *)object;
  g_autoptr(GError) error = NULL;

  g_assert (G_IS_OBJECT (object));
  g_assert (G_IS_ASYNC_RESULT (result));

  rest_oauth2_proxy_refresh_access_token_finish (oauth2_proxy, result, &error);
  if (error)
    g_error ("%s", error->message);

  g_print ("Access Token: %s\n", rest_oauth2_proxy_get_access_token (oauth2_proxy));
  g_print ("Refresh Token: %s\n", rest_oauth2_proxy_get_refresh_token (oauth2_proxy));
  GDateTime *expiration_date = rest_oauth2_proxy_get_expiration_date (oauth2_proxy);
  if (expiration_date)
    g_print ("Expires in: %s\n", g_date_time_format (expiration_date, "%X %x"));

  load_projects (REST_PROXY (oauth2_proxy));
}

static void
gitlab_oauth2_example_fetched_access_token (GObject      *object,
                                            GAsyncResult *result,
                                            gpointer      user_data)
{
  RestOAuth2Proxy *oauth2_proxy = (RestOAuth2Proxy *)object;
  g_autoptr(GError) error = NULL;

  g_assert (G_IS_OBJECT (object));
  g_assert (G_IS_ASYNC_RESULT (result));

  rest_oauth2_proxy_fetch_access_token_finish (oauth2_proxy, result, &error);
  if (error)
    g_error ("%s", error->message);

  g_print ("Access Token: %s\n", rest_oauth2_proxy_get_access_token (oauth2_proxy));
  g_print ("Refresh Token: %s\n", rest_oauth2_proxy_get_refresh_token (oauth2_proxy));
  GDateTime *expiration_date = rest_oauth2_proxy_get_expiration_date (oauth2_proxy);
  if (expiration_date)
    g_print ("Expires in: %s\n", g_date_time_format (expiration_date, "%X %x"));

  /* refresh token */
  rest_oauth2_proxy_refresh_access_token_async (oauth2_proxy, NULL, gitlab_oauth2_example_refreshed_access_token, user_data);
}

gint
main (gint   argc,
      gchar *argv[])
{
  g_autofree gchar *line = NULL;
  size_t len = 0;

  gchar **environment = g_get_environ ();
  gchar *authurl = "https://gitlab.gnome.org/oauth/authorize";
  gchar *tokenurl = "https://gitlab.gnome.org/oauth/token";
  gchar *redirecturl = "http://example.com";
  gchar *baseurl = "https://gitlab.gnome.org/api/v4/";
  const gchar *clientid = g_environ_getenv (environment, "REST_OAUTH2_CLIENT_ID");
  if (!clientid)
    {
      g_print ("You have to define your Gitlab Client ID as REST_OAUTH2_CLIENT_ID environment variable\n");
      return EXIT_SUCCESS;
    }

  const gchar *clientsecret = g_environ_getenv (environment, "REST_OAUTH2_CLIENT_SECRET");
  if (!clientsecret)
    {
      g_print ("You have to define your Gitlab Client Secret as REST_OAUTH2_CLIENT_SECRET environment variable\n");
      return EXIT_SUCCESS;
    }
  RestPkceCodeChallenge *pkce = rest_pkce_code_challenge_new_random ();
  gchar *state = NULL;

#ifdef WITH_SOUP_2
  SoupLogger *logger = soup_logger_new (SOUP_LOGGER_LOG_HEADERS, -1);
#else
  SoupLogger *logger = soup_logger_new (SOUP_LOGGER_LOG_HEADERS);
#endif

  RestOAuth2Proxy *oauth2_proxy = rest_oauth2_proxy_new (authurl, tokenurl, redirecturl, clientid, clientsecret, baseurl);
  rest_proxy_add_soup_feature (REST_PROXY (oauth2_proxy), SOUP_SESSION_FEATURE (logger));
  const gchar *authorize_url = rest_oauth2_proxy_build_authorization_url (oauth2_proxy, rest_pkce_code_challenge_get_challenge (pkce), NULL, &state);

  g_print ("URL to authorize: %s\n", authorize_url);

  ssize_t chars = getline (&line, &len, stdin);
  if (line[chars - 1] == '\n') {
    line[chars - 1] = '\0';
  }

  g_print ("Got Authorization Grant: %s\n", line);

  /* fetch access token */
  rest_oauth2_proxy_fetch_access_token_async (oauth2_proxy, line, rest_pkce_code_challenge_get_verifier (pkce), NULL, gitlab_oauth2_example_fetched_access_token, NULL);

  loop = g_main_loop_new (NULL, FALSE);
  g_main_loop_run (loop);

  return 0;
}