-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoauth_handler.h
44 lines (38 loc) · 1.45 KB
/
oauth_handler.h
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
#ifndef OAUTH_HANDLER_H
#define OAUTH_HANDLER_H
#include <Arduino.h>
#include <HTTPClient.h>
#include <ArduinoJson.h>
#include <WebServer.h>
#include <Preferences.h>
class OAuthHandler {
public:
OAuthHandler(const String& clientId, const String& clientSecret, const String& redirectUri);
void begin(WebServer* server = nullptr);
void handleOAuthRequest(WebServer* server);
bool exchangeAuthCode(const String& code, String& error);
String getAccessToken() { return access_token; }
bool hasValidToken() { return !access_token.isEmpty(); }
bool isAuthorized();
bool getValidToken(String& token);
String getAuthUrl();
String loadRefreshToken();
private:
const String GOOGLE_CLIENT_ID;
const String GOOGLE_CLIENT_SECRET;
const String GOOGLE_REDIRECT_URI;
const String AUTH_ENDPOINT = "https://accounts.google.com/o/oauth2/v2/auth";
const String TOKEN_ENDPOINT = "https://oauth2.googleapis.com/token";
const String SCOPE = "https://www.googleapis.com/auth/calendar.readonly";
String access_token;
String refresh_token;
unsigned long token_expiry;
Preferences preferences;
bool prefsInitialized = false;
WebServer* server;
bool serverAvailable;
void handleTokenRequest(WebServer* server);
bool refreshAccessToken();
void saveRefreshToken(const String& token);
};
#endif