-
Notifications
You must be signed in to change notification settings - Fork 1
/
client_login_client.c
118 lines (100 loc) · 3.16 KB
/
client_login_client.c
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
// Copyright 2010 Garret Kelly, All Rights Reserved
// Author: gkelly@gkelly.org (Garret Kelly)
#include "client_login_client.h"
#include <curl/curl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
static const char *kClientLoginUrl =
"https://www.google.com/accounts/ClientLogin";
static const char *kEmail = "Email";
static const char *kPassword = "Passwd";
static const char *kService = "service";
static const char *kSource = "source";
static const char *kAuthStart = "Auth=";
struct client_login_client {
char *email;
char *password;
char *service;
char *source;
};
client_login_client *client_login_client_new() {
client_login_client *client =
(client_login_client *)malloc(sizeof(client_login_client));
client->email = NULL;
client->password = NULL;
client->service = NULL;
client->source = NULL;
return client;
}
void client_login_client_free(client_login_client *client) {
free(client->email);
free(client->password);
free(client->service);
free(client->source);
free(client);
}
void client_login_client_set_email(client_login_client *client,
const char *email) {
free(client->email);
client->email = strdup(email);
}
void client_login_client_set_password(client_login_client *client,
const char *password) {
free(client->password);
client->password = strdup(password);
}
void client_login_client_set_service(client_login_client *client,
const char *service) {
free(client->service);
client->service = strdup(service);
}
void client_login_client_set_source(client_login_client *client,
const char *source) {
free(client->source);
client->source = strdup(source);
}
int client_login_client_authenticate(
client_login_client *client, char **auth_token) {
struct curl_httppost *post = NULL;
struct curl_httppost *last = NULL;
curl_formadd(&post, &last, CURLFORM_PTRNAME, kEmail,
CURLFORM_PTRCONTENTS, client->email, CURLFORM_END);
curl_formadd(&post, &last, CURLFORM_PTRNAME, kPassword,
CURLFORM_PTRCONTENTS, client->password, CURLFORM_END);
curl_formadd(&post, &last, CURLFORM_PTRNAME, kService,
CURLFORM_PTRCONTENTS, client->service, CURLFORM_END);
if (client->source) {
curl_formadd(&post, &last, CURLFORM_PTRNAME, kSource,
CURLFORM_PTRCONTENTS, client->source, CURLFORM_END);
}
char *response = NULL;
size_t response_length;
FILE *response_stream = open_memstream(&response, &response_length);
if (!response_stream) {
curl_formfree(post);
return 0;
}
CURL *curl_handle = curl_easy_init();
curl_easy_setopt(curl_handle, CURLOPT_URL, kClientLoginUrl);
curl_easy_setopt(curl_handle, CURLOPT_HTTPPOST, post);
curl_easy_setopt(curl_handle, CURLOPT_WRITEDATA, response_stream);
curl_easy_perform(curl_handle);
curl_easy_cleanup(curl_handle);
curl_formfree(post);
fclose(response_stream);
char *auth_value_start = strstr(response, kAuthStart);
if (!auth_value_start) {
free(response);
return 0;
}
char *auth_value_end = strchr(auth_value_start, '\n');
if (!auth_value_end) {
free(response);
return 0;
}
*auth_value_end = '\0';
*auth_token = strdup(auth_value_start + strlen(kAuthStart));
free(response);
return 1;
}