Skip to content

Commit c9c6ea3

Browse files
up
1 parent 7456041 commit c9c6ea3

File tree

877 files changed

+80916
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

877 files changed

+80916
-0
lines changed

Core-Content/FIREBASE/firebase-js-sdk-master/packages-exp/auth-compat-exp/demo/public/index.html

Lines changed: 767 additions & 0 deletions
Large diffs are not rendered by default.
Lines changed: 178 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,178 @@
1+
/**
2+
* @license
3+
* Copyright 2017 Google LLC
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
/**
19+
* @fileoverview Service worker for Firebase Auth test app application. The
20+
* service worker caches all content and only serves cached content in offline
21+
* mode.
22+
*/
23+
24+
importScripts('/dist/firebase-app.js');
25+
importScripts('/dist/firebase-auth.js');
26+
importScripts('config.js');
27+
28+
// Initialize the Firebase app in the web worker.
29+
firebase.initializeApp(config);
30+
31+
var CACHE_NAME = 'cache-v1';
32+
var urlsToCache = [
33+
'/',
34+
'/manifest.json',
35+
'/config.js',
36+
'/script.js',
37+
'/common.js',
38+
'/style.css',
39+
'/dist/firebase-app.js',
40+
'/dist/firebase-auth.js',
41+
'/dist/firebase-database.js'
42+
];
43+
44+
/**
45+
* Returns a promise that resolves with an ID token if available.
46+
* @return {!Promise<?string>} The promise that resolves with an ID token if
47+
* available. Otherwise, the promise resolves with null.
48+
*/
49+
var getIdToken = function () {
50+
return new Promise(function (resolve, reject) {
51+
firebase.auth().onAuthStateChanged(function (user) {
52+
if (user) {
53+
user.getIdToken().then(
54+
function (idToken) {
55+
resolve(idToken);
56+
},
57+
function (error) {
58+
resolve(null);
59+
}
60+
);
61+
} else {
62+
resolve(null);
63+
}
64+
});
65+
}).catch(function (error) {
66+
console.log(error);
67+
});
68+
};
69+
70+
/**
71+
* @param {string} url The URL whose origin is to be returned.
72+
* @return {string} The origin corresponding to given URL.
73+
*/
74+
var getOriginFromUrl = function (url) {
75+
// https://stackoverflow.com/questions/1420881/how-to-extract-base-url-from-a-string-in-javascript
76+
var pathArray = url.split('/');
77+
var protocol = pathArray[0];
78+
var host = pathArray[2];
79+
return protocol + '//' + host;
80+
};
81+
82+
self.addEventListener('install', function (event) {
83+
// Perform install steps.
84+
event.waitUntil(
85+
caches.open(CACHE_NAME).then(function (cache) {
86+
// Add all URLs of resources we want to cache.
87+
return cache.addAll(urlsToCache).catch(function (error) {
88+
// Suppress error as some of the files may not be available for the
89+
// current page.
90+
});
91+
})
92+
);
93+
});
94+
95+
// As this is a test app, let's only return cached data when offline.
96+
self.addEventListener('fetch', function (event) {
97+
var fetchEvent = event;
98+
var requestProcessor = function (idToken) {
99+
var req = event.request;
100+
// For same origin https requests, append idToken to header.
101+
if (
102+
self.location.origin == getOriginFromUrl(event.request.url) &&
103+
(self.location.protocol == 'https:' ||
104+
self.location.hostname == 'localhost') &&
105+
idToken
106+
) {
107+
// Clone headers as request headers are immutable.
108+
var headers = new Headers();
109+
for (var entry of req.headers.entries()) {
110+
headers.append(entry[0], entry[1]);
111+
}
112+
// Add ID token to header. We can't add to Authentication header as it
113+
// will break HTTP basic authentication.
114+
headers.append('x-id-token', idToken);
115+
try {
116+
req = new Request(req.url, {
117+
method: req.method,
118+
headers: headers,
119+
mode: 'same-origin',
120+
credentials: req.credentials,
121+
cache: req.cache,
122+
redirect: req.redirect,
123+
referrer: req.referrer,
124+
body: req.body,
125+
bodyUsed: req.bodyUsed,
126+
context: req.context
127+
});
128+
} catch (e) {
129+
// This will fail for CORS requests. We just continue with the
130+
// fetch caching logic below and do not pass the ID token.
131+
}
132+
}
133+
return fetch(req)
134+
.then(function (response) {
135+
// Check if we received a valid response.
136+
// If not, just funnel the error response.
137+
if (!response || response.status !== 200 || response.type !== 'basic') {
138+
return response;
139+
}
140+
// If response is valid, clone it and save it to the cache.
141+
var responseToCache = response.clone();
142+
// Save response to cache.
143+
caches.open(CACHE_NAME).then(function (cache) {
144+
cache.put(fetchEvent.request, responseToCache);
145+
});
146+
// After caching, return response.
147+
return response;
148+
})
149+
.catch(function (error) {
150+
// For fetch errors, attempt to retrieve the resource from cache.
151+
return caches.match(fetchEvent.request.clone());
152+
})
153+
.catch(function (error) {
154+
// If error getting resource from cache, do nothing.
155+
console.log(error);
156+
});
157+
};
158+
// Try to fetch the resource first after checking for the ID token.
159+
event.respondWith(getIdToken().then(requestProcessor, requestProcessor));
160+
});
161+
162+
self.addEventListener('activate', function (event) {
163+
// Update this list with all caches that need to remain cached.
164+
var cacheWhitelist = ['cache-v1'];
165+
event.waitUntil(
166+
caches.keys().then(function (cacheNames) {
167+
return Promise.all(
168+
cacheNames.map(function (cacheName) {
169+
// Check if cache is not whitelisted above.
170+
if (cacheWhitelist.indexOf(cacheName) === -1) {
171+
// If not whitelisted, delete it.
172+
return caches.delete(cacheName);
173+
}
174+
})
175+
);
176+
})
177+
);
178+
});
Lines changed: 210 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,210 @@
1+
/**
2+
* @license
3+
* Copyright 2018 Google LLC
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
/**
19+
* @fileoverview Web worker for Firebase Auth test app application. The
20+
* web worker tries to run operations on the Auth instance for testing purposes.
21+
*/
22+
23+
importScripts('/dist/firebase-app.js');
24+
importScripts('/dist/firebase-auth.js');
25+
importScripts('config.js');
26+
27+
// Initialize the Firebase app in the web worker.
28+
firebase.initializeApp(config);
29+
30+
/**
31+
* Returns a promise that resolves with an ID token if available.
32+
* @return {!Promise<?string>} The promise that resolves with an ID token if
33+
* available. Otherwise, the promise resolves with null.
34+
*/
35+
var getIdToken = function () {
36+
return new Promise(function (resolve, reject) {
37+
firebase.auth().onAuthStateChanged(function (user) {
38+
if (user) {
39+
user.getIdToken().then(
40+
function (idToken) {
41+
resolve(idToken);
42+
},
43+
function (error) {
44+
resolve(null);
45+
}
46+
);
47+
} else {
48+
resolve(null);
49+
}
50+
});
51+
}).catch(function (error) {
52+
console.log(error);
53+
});
54+
};
55+
56+
/**
57+
* Runs various Firebase Auth tests in a web worker environment and confirms the
58+
* expected behavior. This is useful for manual testing in different browsers.
59+
* @param {string} googleIdToken The Google ID token to sign in with.
60+
* @return {!Promise<void>} A promise that resolves when all tests run
61+
* successfully.
62+
*/
63+
var runWorkerTests = function (googleIdToken) {
64+
var inMemoryPersistence = firebase.auth.Auth.Persistence.NONE;
65+
var expectedDisplayName = 'Test User';
66+
var oauthCredential = firebase.auth.GoogleAuthProvider.credential(
67+
googleIdToken
68+
);
69+
var provider = new firebase.auth.GoogleAuthProvider();
70+
var OPERATION_NOT_SUPPORTED_CODE =
71+
'auth/operation-not-supported-in-this-environment';
72+
var email =
73+
'user' +
74+
Math.floor(Math.random() * 10000000000).toString() +
75+
'@example.com';
76+
var pass = 'password';
77+
return firebase
78+
.auth()
79+
.setPersistence(inMemoryPersistence)
80+
.then(function () {
81+
firebase.auth().useDeviceLanguage();
82+
return firebase.auth().signInAnonymously();
83+
})
84+
.then(function (result) {
85+
if (!result.user.uid) {
86+
throw new Error('signInAnonymously unexpectedly failed!');
87+
}
88+
return result.user.updateProfile({ displayName: expectedDisplayName });
89+
})
90+
.then(function () {
91+
if (firebase.auth().currentUser.displayName != expectedDisplayName) {
92+
throw new Error('Profile update failed!');
93+
}
94+
return firebase.auth().currentUser.delete();
95+
})
96+
.then(function () {
97+
if (firebase.auth().currentUser) {
98+
throw new Error('currentUser.delete unexpectedly failed!');
99+
}
100+
return firebase.auth().createUserWithEmailAndPassword(email, pass);
101+
})
102+
.then(function (result) {
103+
if (result.user.email != email) {
104+
throw new Error('createUserWithEmailAndPassword unexpectedly failed!');
105+
}
106+
return firebase.auth().fetchProvidersForEmail(email);
107+
})
108+
.then(function (providers) {
109+
if (providers.length == 0 || providers[0] != 'password') {
110+
throw new Error('fetchProvidersForEmail failed!');
111+
}
112+
return firebase.auth().signInWithEmailAndPassword(email, pass);
113+
})
114+
.then(function (result) {
115+
if (result.user.email != email) {
116+
throw new Error('signInWithEmailAndPassword unexpectedly failed!');
117+
}
118+
return result.user.delete();
119+
})
120+
.then(function () {
121+
return firebase
122+
.auth()
123+
.signInWithPopup(provider)
124+
.catch(function (error) {
125+
if (error.code != OPERATION_NOT_SUPPORTED_CODE) {
126+
throw error;
127+
}
128+
});
129+
})
130+
.then(function () {
131+
return firebase
132+
.auth()
133+
.signInWithRedirect(provider)
134+
.catch(function (error) {
135+
if (error.code != OPERATION_NOT_SUPPORTED_CODE) {
136+
throw error;
137+
}
138+
});
139+
})
140+
.then(function () {
141+
return Promise.resolve()
142+
.then(function () {
143+
return new firebase.auth.RecaptchaVerifier('id');
144+
})
145+
.then(function () {
146+
throw new Error(
147+
'RecaptchaVerifer instantiation succeeded unexpectedly!'
148+
);
149+
})
150+
.catch(function (error) {
151+
if (error.code != OPERATION_NOT_SUPPORTED_CODE) {
152+
throw error;
153+
}
154+
});
155+
})
156+
.then(function () {
157+
return firebase.auth().signInWithCredential(oauthCredential);
158+
})
159+
.then(function (result) {
160+
if (
161+
!result.user ||
162+
!result.user.uid ||
163+
!result.credential ||
164+
!result.additionalUserInfo
165+
) {
166+
throw new Error('signInWithCredential unexpectedly failed!');
167+
}
168+
return firebase.auth().signOut();
169+
})
170+
.then(function () {
171+
if (firebase.auth().currentUser) {
172+
throw new Error('signOut unexpectedly failed!');
173+
}
174+
});
175+
};
176+
177+
/**
178+
* Handles the incoming message from the main script.
179+
* @param {!Object} e The message event received.
180+
*/
181+
self.onmessage = function (e) {
182+
if (e.data && e.data.type) {
183+
var result = { type: e.data.type };
184+
switch (e.data.type) {
185+
case 'GET_USER_INFO':
186+
getIdToken().then(function (idToken) {
187+
result.idToken = idToken;
188+
result.uid =
189+
firebase.auth().currentUser && firebase.auth().currentUser.uid;
190+
self.postMessage(result);
191+
});
192+
break;
193+
case 'RUN_TESTS':
194+
runWorkerTests(e.data.googleIdToken)
195+
.then(function () {
196+
result.status = 'success';
197+
self.postMessage(result);
198+
})
199+
.catch(function (error) {
200+
result.status = 'failure';
201+
// DataCloneError when postMessaging in IE11 and 10.
202+
result.error = error.code ? error : error.message;
203+
self.postMessage(result);
204+
});
205+
break;
206+
default:
207+
self.postMessage({});
208+
}
209+
}
210+
};

0 commit comments

Comments
 (0)