Skip to content

Commit 061282a

Browse files
authored
Merge pull request #539 from mapswipe/dev
Release OSM Login Functions to production
2 parents af3e498 + 9bc6d02 commit 061282a

File tree

24 files changed

+3144
-30
lines changed

24 files changed

+3144
-30
lines changed

.github/workflows/actions.yml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,7 @@ jobs:
4141
FIREBASE_TOKEN: ${{ secrets.FIREBASE_TOKEN }}
4242
FIREBASE_DB: ${{ secrets.FIREBASE_DB }}
4343
run: |
44-
# Create a mock file for wal-g setup
45-
docker-compose up --build firebase_deploy
44+
docker-compose run firebase_deploy sh -c "firebase use $FIREBASE_DB && firebase deploy --token $FIREBASE_TOKEN --only database:rules"
4645
- name: Decrypt Service Account Key File
4746
working-directory: ./
4847
run: |

docker-compose.yaml

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,19 @@ services:
198198
environment:
199199
FIREBASE_TOKEN: '${FIREBASE_TOKEN}'
200200
FIREBASE_DB: '${FIREBASE_DB}'
201-
command: sh -c "firebase use $FIREBASE_DB && firebase deploy --token $FIREBASE_TOKEN --only functions,database:rules"
202-
volumes:
203-
- ./firebase:/firebase
201+
FIREBASE_AUTH_SITE: '${FIREBASE_AUTH_SITE}'
202+
OSM_OAUTH_REDIRECT_URI: '${OSM_OAUTH_REDIRECT_URI}'
203+
OSM_OAUTH_APP_LOGIN_LINK: '${OSM_OAUTH_APP_LOGIN_LINK}'
204+
OSM_OAUTH_API_URL: '${OSM_OAUTH_API_URL}'
205+
OSM_OAUTH_CLIENT_ID: '${OSM_OAUTH_CLIENT_ID}'
206+
OSM_OAUTH_CLIENT_SECRET: '${OSM_OAUTH_CLIENT_SECRET}'
207+
command: >-
208+
sh -c "firebase use $FIREBASE_DB &&
209+
firebase target:apply hosting auth \"$FIREBASE_AUTH_SITE\" &&
210+
firebase functions:config:set
211+
osm.redirect_uri=\"$OSM_OAUTH_REDIRECT_URI\"
212+
osm.app_login_link=\"$OSM_OAUTH_APP_LOGIN_LINK\"
213+
osm.api_url=\"$OSM_OAUTH_API_URL\"
214+
osm.client_id=\"$OSM_OAUTH_CLIENT_ID\"
215+
osm.client_secret=\"$OSM_OAUTH_CLIENT_SECRET\" &&
216+
firebase deploy --token $FIREBASE_TOKEN --only functions,hosting,database:rules"

docs/source/dev_setup.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,9 @@ On how to setup the development environment and how to deploy functions to the F
143143
For more information refer to the official [Reference on Cloud Function for Firebase](https://firebase.google.com/docs/reference/functions/).
144144
For example function take a look at this [GitHub repository](https://github.com/firebase/functions-samples).
145145

146+
### OSM OAuth 2
147+
Firebase functions are also used to allow users to login to MapSwipe with their OpenStreetMap account. Refer to [the notes in the app repository](https://github.com/mapswipe/mapswipe/blob/master/docs/osm_login.md) for more information.
148+
146149

147150
## Database Backup
148151

example.env

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ FIREBASE_DB=
33
FIREBASE_API_KEY=
44
FIREBASE_TOKEN=
55
GOOGLE_APPLICATION_CREDENTIALS="$HOME/.config/mapswipe_workers/serviceAccountKey.json"
6+
FIREBASE_AUTH_SITE=
67

78
# postgres configuration
89
POSTGRES_USER=mapswipe_workers
@@ -31,3 +32,10 @@ SENTRY_DSN=
3132

3233
# osmcha configuration
3334
OSMCHA_API_KEY=
35+
36+
# OSM OAuth Configuration
37+
OSM_OAUTH_REDIRECT_URI=
38+
OSM_OAUTH_API_URL=
39+
OSM_OAUTH_CLIENT_ID=
40+
OSM_OAUTH_CLIENT_SECRET=
41+
OSM_APP_LOGIN_LINK=

firebase/.firebaserc

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,14 @@
11
{
22
"projects": {
33
"default": "dev-mapswipe"
4+
},
5+
"targets": {
6+
"dev-mapswipe": {
7+
"hosting": {
8+
"auth": [
9+
"dev-auth-mapswipe"
10+
]
11+
}
12+
}
413
}
5-
}
14+
}

firebase/.prettierrc

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
trailingComma: "all"
2+
tabWidth: 4
3+
singleQuote: true
4+
arrowParens: "avoid"

firebase/Dockerfile

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
FROM node:12-alpine
1+
FROM node:14-alpine
22
RUN npm install firebase-functions@latest firebase-admin@latest --save
33
RUN npm install -g firebase-tools
4+
COPY . /firebase
5+
RUN cd firebase/functions && npm install
46
WORKDIR /firebase/

firebase/README.md

Lines changed: 47 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,51 @@ Then run the container interactively and open a bash shell.
1515
Now you are inside the docker container and can login to firebase. You need to insert an authorization code into the terminal during that process.
1616
* `firebase login --no-localhost`
1717

18-
Finally you can deploy your changes for cloud functions and database rules individually.
19-
* `firebase deploy --only functions`
18+
Finally you can deploy your changes for cloud functions and database rules individually. Hosting must be done as well to
19+
expose the authentication functions publicly.
20+
* `firebase deploy --only functions,hosting`
2021
* `firebase deploy --only database:rules`
22+
23+
## Notes on OAuth (OSM login)
24+
25+
Refer to [the notes in the app repository](https://github.com/mapswipe/mapswipe/blob/master/docs/osm_login.md).
26+
27+
Some specifics about the related functions:
28+
- get a service-account.json file from firebase which allows the OAuth functions to access the database and call
29+
external URLs (this last point only works on a firebase Blaze plan)
30+
- Before deploying, set the required firebase config values in environment:
31+
FIXME: replace env vars with config value names
32+
- OSM_OAUTH_REDIRECT_URI `osm.redirect_uri`: `https://dev-auth.mapswipe.org/token` or `https://auth.mapswipe.org/token`
33+
- OSM_OAUTH_APP_LOGIN_LINK `osm.app_login_link`: 'devmapswipe://login/osm' or 'mapswipe://login/osm'
34+
- OSM_OAUTH_API_URL `osm.api_url`: 'https://master.apis.dev.openstreetmap.org/' or 'https://www.openstreetmap.org/' (include the
35+
trailing slash)
36+
- OSM_OAUTH_CLIENT_ID `osm.client_id`: find it on the OSM application page
37+
- OSM_OAUTH_CLIENT_SECRET `osm.client_secret`: same as above. Note that this can only be seen once when the application is created. Do not
38+
lose it!
39+
- Deploy the functions as explained above
40+
- Expose the functions publicly through firebase hosting, this is done in `/firebase/firebase.json` under the `hosting`
41+
key.
42+
43+
The functions must be publicly exposed to allow anyone to run them without authentication, after they have first been
44+
deployed:
45+
- in firebase console, open the [list of cloud
46+
functions](https://console.cloud.google.com/functions/list?project=dev-mapswipe&authuser=0&hl=en&tab=permissions)
47+
- "allow unauthenticated" is not visible in the "authentication" column, then
48+
- select the auth functions by checking the box to the left side of them in the list
49+
- click "permissions" near the top, then "Add principal"
50+
- under "new principal" pick "allUsers"
51+
- under "select a role, choose "Cloud Function Invoker" and save.
52+
- Confirm all the warnings
53+
54+
See https://firebase.google.com/docs/functions/http-events#invoke_an_http_function for the full story (and
55+
https://cloud.google.com/functions/docs/securing/managing-access-iam#allowing_unauthenticated_http_function_invocation).
56+
If you don't do this, you will get an HTTP 403 error saying you don't have permission to access the function.
57+
58+
You also need to enable the "IAM service account credentials API" by going to
59+
https://console.cloud.google.com/apis/api/iamcredentials.googleapis.com/credentials?project=dev-mapswipe.
60+
61+
Finally, you need to figure out the service account used by the cloud functions (it apparently is `[email protected]` by default) and grant it the right to sign blobs, see https://firebase.google.com/docs/auth/admin/create-custom-tokens#service_account_does_not_have_required_permissions.
62+
63+
We store the user's OSM access token in the database, which right now does not do anything, but would be needed if we
64+
want our backend to do something in OSM on behalf of the user. The database access rules are set to only allow the owner
65+
of a token to access them.

firebase/database.rules.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,12 @@
137137
"created",
138138
"teamId"
139139
]
140+
},
141+
"OSMAccessToken": {
142+
"$uid": {
143+
".read": "auth.uid === $uid",
144+
".write": "auth.uid === $uid"
145+
}
140146
}
141147
},
142148
// leaving this here, since version before v2 pull data from there

firebase/firebase.json

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,19 @@
55
},
66
"database": {
77
"rules": "database.rules.json"
8+
},
9+
"hosting": {
10+
"target": "auth",
11+
"public": "public",
12+
"rewrites": [
13+
{
14+
"source": "/redirect",
15+
"function": "osmAuth-redirect"
16+
},
17+
{
18+
"source": "/token",
19+
"function": "osmAuth-token"
20+
}
21+
]
822
}
923
}

0 commit comments

Comments
 (0)