Firebase Auth, oturum yönetimi için Firebase kimlik jetonlarını algılayıp iletmek üzere hizmet çalışanlarını kullanma olanağı sunar. Bu sayede aşağıdaki avantajlardan yararlanabilirsiniz:
- Ek bir işlem yapmadan sunucudan gelen her HTTP isteğinde kimlik jetonu iletebilme.
- Kimlik jetonunu ek bir gidiş dönüş veya gecikme olmadan yenileyebilme.
- Arka uç ve ön uç senkronize oturumlar. Realtime Database, Firestore gibi Firebase hizmetlerine ve bazı harici sunucu tarafı kaynaklara (SQL veritabanı vb.) erişmesi gereken uygulamalar bu çözümü kullanabilir. Ayrıca aynı oturuma hizmet çalışanından, web çalışanından veya paylaşılan çalışandan da erişilebilir.
- Firebase Auth kaynak kodunu her sayfaya ekleme ihtiyacını ortadan kaldırır (gecikmeyi azaltır). Bir kez yüklenip başlatılan hizmet çalışanı, arka planda tüm istemciler için oturum yönetimini gerçekleştirir.
Genel Bakış
Firebase Auth, istemci tarafında çalışacak şekilde optimize edilmiştir. Jetonlar web depolama alanına kaydedilir. Bu sayede Realtime Database, Cloud Firestore, Cloud Storage gibi diğer Firebase hizmetleriyle de kolayca entegre edilebilir. Oturumları sunucu tarafı perspektifinden yönetmek için kimlik jetonlarının alınıp sunucuya iletilmesi gerekir.
Web
import { getAuth, getIdToken } from "firebase/auth"; const auth = getAuth(); getIdToken(auth.currentUser) .then((idToken) => { // idToken can be passed back to server. }) .catch((error) => { // Error occurred. });
Web
firebase.auth().currentUser.getIdToken() .then((idToken) => { // idToken can be passed back to server. }) .catch((error) => { // Error occurred. });
Ancak bu, en son kimlik jetonunu almak ve ardından istek başlığı, POST gövdesi vb. aracılığıyla sunucuya iletmek için istemciden bir komut dosyasının çalıştırılması gerektiği anlamına gelir.
Bu yöntem ölçeklendirilemeyebilir ve bunun yerine sunucu tarafı oturum çerezleri gerekebilir. Kimlik jetonları oturum çerezleri olarak ayarlanabilir ancak bunlar kısa ömürlüdür ve istemciden yenilenmesi, ardından süresi dolduğunda yeni çerezler olarak ayarlanması gerekir. Bu durum, kullanıcı siteyi bir süredir ziyaret etmediyse ek bir gidiş dönüş gerektirebilir.
Firebase Auth daha geleneksel bir çerez tabanlı oturum yönetimi çözümü sunsa da bu çözüm, sunucu tarafı httpOnly çerez tabanlı uygulamalar için en iyi sonucu verir. Ayrıca istemci jetonları ile sunucu tarafı jetonlarının senkronizasyonu bozulabileceğinden (özellikle istemci tabanlı diğer Firebase hizmetlerini de kullanmanız gerekiyorsa) bu çözümün yönetimi daha zordur.
Bunun yerine, sunucu tarafı tüketim için kullanıcı oturumlarını yönetmek üzere hizmet çalışanları kullanılabilir. Bunun nedeni şunlardır:
- Hizmet çalışanları, mevcut Firebase Auth durumuna erişebilir. Mevcut kullanıcı kimliği jetonu, hizmet çalışanından alınabilir. Jetonun süresi dolmuşsa istemci SDK'sı jetonu yeniler ve yeni bir jeton döndürür.
- Hizmet çalışanları, getirme isteklerini yakalayıp değiştirebilir.
Hizmet çalışanı değişiklikleri
Hizmet çalışanının, Auth kitaplığını ve oturum açmış bir kullanıcının mevcut kimlik jetonunu alma özelliğini içermesi gerekir.
Web
import { initializeApp } from "firebase/app"; import { getAuth, onAuthStateChanged, getIdToken } from "firebase/auth"; // Initialize the Firebase app in the service worker script. initializeApp(config); /** * Returns a promise that resolves with an ID token if available. * @return {!Promise<?string>} The promise that resolves with an ID token if * available. Otherwise, the promise resolves with null. */ const auth = getAuth(); const getIdTokenPromise = () => { return new Promise((resolve, reject) => { const unsubscribe = onAuthStateChanged(auth, (user) => { unsubscribe(); if (user) { getIdToken(user).then((idToken) => { resolve(idToken); }, (error) => { resolve(null); }); } else { resolve(null); } }); }); };
Web
// Initialize the Firebase app in the service worker script. firebase.initializeApp(config); /** * Returns a promise that resolves with an ID token if available. * @return {!Promise<?string>} The promise that resolves with an ID token if * available. Otherwise, the promise resolves with null. */ const getIdToken = () => { return new Promise((resolve, reject) => { const unsubscribe = firebase.auth().onAuthStateChanged((user) => { unsubscribe(); if (user) { user.getIdToken().then((idToken) => { resolve(idToken); }, (error) => { resolve(null); }); } else { resolve(null); } }); }); };
Uygulamanın kaynağına gönderilen tüm getirme istekleri yakalanır ve bir kimlik jetonu varsa üstbilgi aracılığıyla isteğe eklenir. Sunucu tarafında, istek başlıkları kimlik jetonu için kontrol edilir, doğrulanır ve işlenir. Hizmet çalışanı komut dosyasında, getirme isteği yakalanır ve değiştirilir.
Web
const getOriginFromUrl = (url) => { // https://stackoverflow.com/questions/1420881/how-to-extract-base-url-from-a-string-in-javascript const pathArray = url.split('/'); const protocol = pathArray[0]; const host = pathArray[2]; return protocol + '//' + host; }; // Get underlying body if available. Works for text and json bodies. const getBodyContent = (req) => { return Promise.resolve().then(() => { if (req.method !== 'GET') { if (req.headers.get('Content-Type').indexOf('json') !== -1) { return req.json() .then((json) => { return JSON.stringify(json); }); } else if ( req.headers.get('Content-Type').indexOf('multipart/form-data') !== -1 ) { return req.clone().arrayBuffer(); } else { return req.text(); } } }).catch((error) => { // Ignore error. }); }; self.addEventListener('fetch', (event) => { /** @type {FetchEvent} */ const evt = event; const requestProcessor = (idToken) => { let req = evt.request; let processRequestPromise = Promise.resolve(); // For same origin https requests, append idToken to header. if (self.location.origin == getOriginFromUrl(evt.request.url) && (self.location.protocol == 'https:' || self.location.hostname == 'localhost') && idToken) { // Clone headers as request headers are immutable. const headers = new Headers(); req.headers.forEach((val, key) => { headers.append(key, val); }); // Add ID token to header. headers.append('Authorization', 'Bearer ' + idToken); processRequestPromise = getBodyContent(req).then((body) => { try { req = new Request(req.url, { method: req.method, headers: headers, mode: 'same-origin', credentials: req.credentials, cache: req.cache, redirect: req.redirect, referrer: req.referrer, body, // bodyUsed: req.bodyUsed, // context: req.context }); } catch (e) { // This will fail for CORS requests. We just continue with the // fetch caching logic below and do not pass the ID token. } }); } return processRequestPromise.then(() => { return fetch(req); }); }; // Fetch the resource after checking for the ID token. // This can also be integrated with existing logic to serve cached files // in offline mode. evt.respondWith(getIdTokenPromise().then(requestProcessor, requestProcessor)); });
Web
const getOriginFromUrl = (url) => { // https://stackoverflow.com/questions/1420881/how-to-extract-base-url-from-a-string-in-javascript const pathArray = url.split('/'); const protocol = pathArray[0]; const host = pathArray[2]; return protocol + '//' + host; }; // Get underlying body if available. Works for text and json bodies. const getBodyContent = (req) => { return Promise.resolve().then(() => { if (req.method !== 'GET') { if (req.headers.get('Content-Type').indexOf('json') !== -1) { return req.json() .then((json) => { return JSON.stringify(json); }); } else if ( req.headers.get('Content-Type').indexOf('multipart/form-data') !== -1 ) { return req.clone().arrayBuffer(); } else { return req.text(); } } }).catch((error) => { // Ignore error. }); }; self.addEventListener('fetch', (event) => { /** @type {FetchEvent} */ const evt = event; const requestProcessor = (idToken) => { let req = evt.request; let processRequestPromise = Promise.resolve(); // For same origin https requests, append idToken to header. if (self.location.origin == getOriginFromUrl(evt.request.url) && (self.location.protocol == 'https:' || self.location.hostname == 'localhost') && idToken) { // Clone headers as request headers are immutable. const headers = new Headers(); req.headers.forEach((val, key) => { headers.append(key, val); }); // Add ID token to header. headers.append('Authorization', 'Bearer ' + idToken); processRequestPromise = getBodyContent(req).then((body) => { try { req = new Request(req.url, { method: req.method, headers: headers, mode: 'same-origin', credentials: req.credentials, cache: req.cache, redirect: req.redirect, referrer: req.referrer, body, // bodyUsed: req.bodyUsed, // context: req.context }); } catch (e) { // This will fail for CORS requests. We just continue with the // fetch caching logic below and do not pass the ID token. } }); } return processRequestPromise.then(() => { return fetch(req); }); }; // Fetch the resource after checking for the ID token. // This can also be integrated with existing logic to serve cached files // in offline mode. evt.respondWith(getIdToken().then(requestProcessor, requestProcessor)); });
Bu nedenle, kimliği doğrulanmış tüm isteklerde her zaman ek işlem yapılmadan üstbilgide bir kimlik jetonu iletilir.
Hizmet çalışanının Auth durum değişikliklerini algılayabilmesi için oturum açma/kaydolma sayfasına yüklenmesi gerekir. Hizmet çalışanının, tarayıcı kapatıldıktan sonra da çalışmaya devam edecek şekilde paketlendiğinden emin olun.
Yükleme işleminden sonra hizmet çalışanının, etkinleştirme sırasında clients.claim() yöntemini çağırması gerekir. Böylece mevcut sayfanın denetleyicisi olarak ayarlanabilir.
Web
self.addEventListener('activate', (event) => { event.waitUntil(clients.claim()); });
Web
self.addEventListener('activate', (event) => { event.waitUntil(clients.claim()); });
İstemci tarafı değişiklikleri
Destekleniyorsa hizmet çalışanının istemci tarafındaki oturum açma/kaydolma sayfasına yüklenmesi gerekir.
Web
// Install servicerWorker if supported on sign-in/sign-up page. if ('serviceWorker' in navigator) { navigator.serviceWorker.register('/service-worker.js', {scope: '/'}); }
Web
// Install servicerWorker if supported on sign-in/sign-up page. if ('serviceWorker' in navigator) { navigator.serviceWorker.register('/service-worker.js', {scope: '/'}); }
Kullanıcı oturum açıp başka bir sayfaya yönlendirildiğinde hizmet çalışanı, yönlendirme tamamlanmadan önce kimlik jetonunu başlığa ekleyebilir.
Web
import { getAuth, signInWithEmailAndPassword } from "firebase/auth"; // Sign in screen. const auth = getAuth(); signInWithEmailAndPassword(auth, email, password) .then((result) => { // Redirect to profile page after sign-in. The service worker will detect // this and append the ID token to the header. window.location.assign('/profile'); }) .catch((error) => { // Error occurred. });
Web
// Sign in screen. firebase.auth().signInWithEmailAndPassword(email, password) .then((result) => { // Redirect to profile page after sign-in. The service worker will detect // this and append the ID token to the header. window.location.assign('/profile'); }) .catch((error) => { // Error occurred. });
Sunucu tarafı değişiklikleri
Sunucu tarafı kodu, her istekte kimlik jetonunu algılayabilir. Bu davranış, Node.js için Admin SDK tarafından veya FirebaseServerApp kullanılarak Web SDK ile desteklenir.
Node.js
// Server side code.
const { initializeApp } = require('firebase-admin/app');
const { getAuth } = require('firebase-admin/auth');
// The Firebase Admin SDK is used here to verify the ID token.
initializeApp();
function getIdToken(req) {
// Parse the injected ID token from the request header.
const authorizationHeader = req.headers.authorization || '';
const components = authorizationHeader.split(' ');
return components.length > 1 ? components[1] : '';
}
function checkIfSignedIn(url) {
return (req, res, next) => {
if (req.url == url) {
const idToken = getIdToken(req);
// Verify the ID token using the Firebase Admin SDK.
// User already logged in. Redirect to profile page.
getAuth().verifyIdToken(idToken).then((decodedClaims) => {
// User is authenticated, user claims can be retrieved from
// decodedClaims.
// In this sample code, authenticated users are always redirected to
// the profile page.
res.redirect('/profile');
}).catch((error) => {
next();
});
} else {
next();
}
};
}
// If a user is signed in, redirect to profile page.
app.use(checkIfSignedIn('/'));
Web modular API
import { initializeServerApp } from 'firebase/app';
import { getAuth } from 'firebase/auth';
import { headers } from 'next/headers';
import { redirect } from 'next/navigation';
export default function MyServerComponent() {
// Get relevant request headers (in Next.JS)
const authIdToken = headers().get('Authorization')?.split('Bearer ')[1];
// Initialize the FirebaseServerApp instance.
const serverApp = initializeServerApp(firebaseConfig, { authIdToken });
// Initialize Firebase Authentication using the FirebaseServerApp instance.
const auth = getAuth(serverApp);
if (auth.currentUser) {
redirect('/profile');
}
// ...
}
Sonuç
Ayrıca, kimlik jetonları hizmet çalışanları aracılığıyla ayarlanacağından ve hizmet çalışanları aynı kaynaktan çalıştırılacak şekilde kısıtlandığından CSRF riski yoktur. Farklı kaynaktan bir web sitesi, uç noktalarınızı çağırmaya çalıştığında hizmet çalışanını çağıramaz ve bu da isteğin sunucu açısından kimliği doğrulanmamış olarak görünmesine neden olur.
Hizmet çalışanları artık tüm modern büyük tarayıcılarda desteklenirken bazı eski tarayıcılar bunları desteklemez. Bu nedenle, hizmet çalışanları kullanılamadığında kimlik jetonunu sunucunuza iletmek için bir yedek gerekebilir veya bir uygulama yalnızca hizmet çalışanlarını destekleyen tarayıcılarda çalışacak şekilde kısıtlanabilir.
Hizmet çalışanlarının yalnızca tek kaynaklı olduğunu ve yalnızca https bağlantısı veya localhost üzerinden sunulan web sitelerine yükleneceğini unutmayın.
Hizmet çalışanı için tarayıcı desteği hakkında daha fazla bilgiyi caniuse.com adresinde bulabilirsiniz.
Faydalı bağlantılar
- Oturum yönetimi için hizmet çalışanlarını kullanma hakkında daha fazla bilgi edinmek istiyorsanız GitHub'daki örnek uygulama kaynak koduna göz atın.
- Yukarıdaki örnek uygulamanın dağıtılmış bir sürümünü https://auth-service-worker.appspot.com adresinde bulabilirsiniz.