Skip to content

Commit 00d3f1d

Browse files
committed
Improve and add more token management functions and examples to support external id and access tokens.
1 parent 664bf7d commit 00d3f1d

File tree

31 files changed

+1472
-751
lines changed

31 files changed

+1472
-751
lines changed

examples/Authentications/ReAuthenticate/ReAuthenticate.ino

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,9 @@ void signIn(const char *email, const char *password)
7373
/* Assign the user sign in credentials */
7474
auth.user.email = email;
7575
auth.user.password = password;
76+
77+
/* Reset stored authen and config */
78+
Firebase.reset(&config);
7679

7780
/* Initialize the library with the Firebase authen and config */
7881
Firebase.begin(&config, &auth);

examples/Authentications/SignInAsAdmin/AccessTokenFile/AccessTokenFile.ino

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@
2121
* OAuth2.0 acces tokens generation will fail
2222
* because of invalid expiration time in JWT token that used in the id/access
2323
* token request.
24+
*
25+
* This library used RFC 7523, JWT Bearer Token Grant Type Profile for OAuth 2.0
26+
* which no refresh token is available for access token exchanging.
2427
*/
2528

2629
#if defined(ESP32)
@@ -132,6 +135,8 @@ void setup()
132135
Firebase.begin(&config, &auth);
133136

134137
/* The access token can be accessed from Firebase.getToken(). */
138+
139+
/* No refresh token is available for OAuth2.0 authentication method used in this library i.e. JWT Bearer Token Grant Type Profile */
135140
}
136141

137142
void loop()

examples/Authentications/SignInAsUser/CustomToken/CustomToken.ino

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -239,6 +239,8 @@ void setup()
239239
* passed to the Firebase.begin function.
240240
*
241241
* The id token can be accessed from Firebase.getToken().
242+
*
243+
* The refresh token can be accessed from Firebase.getRefreshToken().
242244
*/
243245
}
244246

examples/Authentications/SignInAsUser/CustomTokenFile/CustomTokenFile.ino

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,8 @@ void setup()
196196
* passed to the Firebase.begin function.
197197
*
198198
* The id token can be accessed from Firebase.getToken().
199+
*
200+
* The refresh token can be accessed from Firebase.getRefreshToken().
199201
*/
200202
}
201203

examples/Authentications/SignInAsUser/EmailPassword/EmailPassword.ino

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,10 @@ void setup()
143143
Firebase.RTDB.setReadWriteRules(&fbdo, base_path, var, val, val, DATABASE_SECRET);
144144

145145
/** path for user data is now "/UsersData/<user uid>"
146-
* The user UID can be taken from auth.token.uid
146+
*
147+
* The id token can be accessed from Firebase.getToken().
148+
*
149+
* The refresh token can be accessed from Firebase.getRefreshToken().
147150
*/
148151
}
149152

@@ -159,5 +162,8 @@ void loop()
159162
path += auth.token.uid.c_str(); //<- user uid of current user that sign in with Emal/Password
160163
path += "/test/int";
161164
Serial.printf("Set int... %s\n", Firebase.RTDB.setInt(&fbdo, path, count++) ? "ok" : fbdo.errorReason().c_str());
165+
166+
// You can use refresh token from Firebase.getRefreshToken() to sign in next time without providing Email and Password.
167+
// See SignInWithRefreshIDToken example.
162168
}
163169
}
Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
2+
/**
3+
* Created by K. Suwatchai (Mobizt)
4+
*
5+
* Email: k_suwatchai@hotmail.com
6+
*
7+
* Github: https://github.com/mobizt/Firebase-ESP-Client
8+
*
9+
* Copyright (c) 2022 mobizt
10+
*
11+
*/
12+
13+
/* This example shows how to authenticate using the OAuth2.0 access token generated from other app. */
14+
15+
#if defined(ESP32)
16+
#include <WiFi.h>
17+
#elif defined(ESP8266)
18+
#include <ESP8266WiFi.h>
19+
#endif
20+
21+
#include <Firebase_ESP_Client.h>
22+
23+
// Provide the token generation process info.
24+
#include <addons/TokenHelper.h>
25+
26+
// Provide the RTDB payload printing info and other helper functions.
27+
#include <addons/RTDBHelper.h>
28+
29+
/* 1. Define the WiFi credentials */
30+
#define WIFI_SSID "WIFI_AP"
31+
#define WIFI_PASSWORD "WIFI_PASSWORD"
32+
33+
/* 2. If work with RTDB, define the RTDB URL */
34+
#define DATABASE_URL "URL" //<databaseName>.firebaseio.com or <databaseName>.<region>.firebasedatabase.app
35+
36+
/* 3. Define the Firebase Data object */
37+
FirebaseData fbdo;
38+
39+
/* 4. Define the FirebaseAuth data for authentication data */
40+
FirebaseAuth auth;
41+
42+
/* 5. Define the FirebaseConfig data for config data */
43+
FirebaseConfig config;
44+
45+
unsigned long dataMillis = 0;
46+
int count = 0;
47+
48+
void setup()
49+
{
50+
51+
Serial.begin(115200);
52+
53+
WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
54+
Serial.print("Connecting to Wi-Fi");
55+
while (WiFi.status() != WL_CONNECTED)
56+
{
57+
Serial.print(".");
58+
delay(300);
59+
}
60+
Serial.println();
61+
Serial.print("Connected with IP: ");
62+
Serial.println(WiFi.localIP());
63+
Serial.println();
64+
65+
Serial.printf("Firebase Client v%s\n\n", FIREBASE_CLIENT_VERSION);
66+
67+
/* Assign the RTDB URL */
68+
config.database_url = DATABASE_URL;
69+
70+
Firebase.reconnectWiFi(true);
71+
72+
/* Assign the callback function for the long running token generation task */
73+
config.token_status_callback = tokenStatusCallback; // see addons/TokenHelper.h
74+
75+
// To refresh the token 5 minutes before expired
76+
config.signer.preRefreshSeconds = 5 * 60;
77+
78+
/* Set access token */
79+
// The access token obtained from other apps using OAuth2.0 authentication.
80+
81+
// The APIs scopes that cover all library applications should include the following
82+
// https://www.googleapis.com/auth/devstorage.full_control
83+
// https://www.googleapis.com/auth/datastore
84+
// https://www.googleapis.com/auth/userinfo.email
85+
// https://www.googleapis.com/auth/firebase.database
86+
// https://www.googleapis.com/auth/cloud-platform
87+
// https://www.googleapis.com/auth/iam
88+
89+
// Refresh token, Client ID and Client Secret are required for OAuth2.0 token refreshing.
90+
// The Client ID and Client Secret can be taken from https://console.developers.google.com/apis/credentials
91+
92+
// If Refresh token was not assigned or empty string, the id token will not refresh when it expired.
93+
Firebase.setAccessToken(&config, "<Access Token>" /* access token */, 3600 /* expiry time */, "<Refresh Token>" /* refresh token */, "<Client ID>" /* client id */, "<Client Secret>" /* client secret */);
94+
95+
Firebase.begin(&config, &auth);
96+
}
97+
98+
void loop()
99+
{
100+
101+
// Firebase.ready() should be called repeatedly to handle authentication tasks.
102+
103+
if (millis() - dataMillis > 5000 && Firebase.ready())
104+
{
105+
dataMillis = millis();
106+
Serial.printf("Set int... %s\n", Firebase.RTDB.setInt(&fbdo, "/test/int", count++) ? "ok" : fbdo.errorReason().c_str());
107+
}
108+
}

examples/Authentications/SignInWithIDToken/SignInWithIDToken.ino

Lines changed: 5 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -84,35 +84,18 @@ void setup()
8484

8585
Firebase.reconnectWiFi(true);
8686

87-
/** To sign in as anonymous user, just sign up as anonymous user
88-
* with blank email and password.
89-
*
90-
* The Anonymous provider must be enabled.
91-
*
92-
* To enable Anonymous provider,
93-
* from Firebase console, select Authentication, select Sign-in method tab,
94-
* under the Sign-in providers list, enable Anonymous provider.
95-
*
96-
* Warning: this will create anonymous user everytime you called this function and your user list
97-
* will grow up and the anonymous users stay in the user list after it created and can be garbage user
98-
* after the generated id token from this anonymous user will not use anymore.
99-
*
100-
* https://stackoverflow.com/questions/38694015/what-happens-to-firebase-anonymous-users
101-
* https://stackoverflow.com/questions/39640574/how-to-bulk-delete-firebase-anonymous-users
102-
*/
87+
/* Assign the callback function for the long running token generation task */
88+
config.token_status_callback = tokenStatusCallback; // see addons/TokenHelper.h
89+
90+
// To refresh the token 5 minutes before expired
91+
config.signer.preRefreshSeconds = 5 * 60;
10392

10493
/* Set ID token */
10594
// The ID token obtained from other apps e.g. Firebase Admin.
10695
// The Refresh token for token refreshment which used when token was expired.
10796
// If Refresh token was not assigned or empty string, the ID token will not refresh when it expired.
10897
Firebase.setIdToken(&config, "<ID Token>", 3600 /* expiry time */, "<Refresh Token>" /* refresh token */);
10998

110-
// To refresh the token 5 minutes before expired
111-
config.signer.preRefreshSeconds = 5 * 60;
112-
113-
/* Assign the callback function for the long running token generation task */
114-
config.token_status_callback = tokenStatusCallback; // see addons/TokenHelper.h
115-
11699
Firebase.begin(&config, &auth);
117100
}
118101

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
2+
/**
3+
* Created by K. Suwatchai (Mobizt)
4+
*
5+
* Email: k_suwatchai@hotmail.com
6+
*
7+
* Github: https://github.com/mobizt/Firebase-ESP-Client
8+
*
9+
* Copyright (c) 2022 mobizt
10+
*
11+
*/
12+
13+
/* This example shows how to re-authenticate using the refresh token generated from other app via OAuth2.0 authentication. */
14+
15+
#if defined(ESP32)
16+
#include <WiFi.h>
17+
#elif defined(ESP8266)
18+
#include <ESP8266WiFi.h>
19+
#endif
20+
21+
#include <Firebase_ESP_Client.h>
22+
23+
// Provide the token generation process info.
24+
#include <addons/TokenHelper.h>
25+
26+
// Provide the RTDB payload printing info and other helper functions.
27+
#include <addons/RTDBHelper.h>
28+
29+
/* 1. Define the WiFi credentials */
30+
#define WIFI_SSID "WIFI_AP"
31+
#define WIFI_PASSWORD "WIFI_PASSWORD"
32+
33+
/* 2. If work with RTDB, define the RTDB URL */
34+
#define DATABASE_URL "URL" //<databaseName>.firebaseio.com or <databaseName>.<region>.firebasedatabase.app
35+
36+
/* 3. Define the Firebase Data object */
37+
FirebaseData fbdo;
38+
39+
/* 4. Define the FirebaseAuth data for authentication data */
40+
FirebaseAuth auth;
41+
42+
/* 5. Define the FirebaseConfig data for config data */
43+
FirebaseConfig config;
44+
45+
unsigned long dataMillis = 0;
46+
int count = 0;
47+
48+
void setup()
49+
{
50+
51+
Serial.begin(115200);
52+
53+
WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
54+
Serial.print("Connecting to Wi-Fi");
55+
while (WiFi.status() != WL_CONNECTED)
56+
{
57+
Serial.print(".");
58+
delay(300);
59+
}
60+
Serial.println();
61+
Serial.print("Connected with IP: ");
62+
Serial.println(WiFi.localIP());
63+
Serial.println();
64+
65+
Serial.printf("Firebase Client v%s\n\n", FIREBASE_CLIENT_VERSION);
66+
67+
/* Assign the RTDB URL */
68+
config.database_url = DATABASE_URL;
69+
70+
Firebase.reconnectWiFi(true);
71+
72+
/* Assign the callback function for the long running token generation task */
73+
config.token_status_callback = tokenStatusCallback; // see addons/TokenHelper.h
74+
75+
// To refresh the token 5 minutes before expired
76+
config.signer.preRefreshSeconds = 5 * 60;
77+
78+
// Use refresh token and force token to refresh immediately
79+
// Refresh token, Client ID and Client Secret are required for OAuth2.0 token refreshing.
80+
// The Client ID and Client Secret can be taken from https://console.developers.google.com/apis/credentials
81+
82+
Firebase.setAccessToken(&config, "" /* set access token to empty to refresh token immediately */, 3600 /* expiry time */, "<Refresh Token>" /* refresh token */, "<Client ID>" /* client id */, "<Client Secret>" /* client secret */);
83+
84+
// Or refresh token manually
85+
// Firebase.refreshToken(&config);
86+
87+
Firebase.begin(&config, &auth);
88+
}
89+
90+
void loop()
91+
{
92+
93+
// Firebase.ready() should be called repeatedly to handle authentication tasks.
94+
95+
if (millis() - dataMillis > 5000 && Firebase.ready())
96+
{
97+
dataMillis = millis();
98+
Serial.printf("Set int... %s\n", Firebase.RTDB.setInt(&fbdo, "/test/int", count++) ? "ok" : fbdo.errorReason().c_str());
99+
}
100+
}

0 commit comments

Comments
 (0)