0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

Oracle Cloud Infrastructure (OCI) API Gateway Firebase Authorizer

Posted at

シリコンバレーのDeveloperWeek2023 HackathonでOCIを使ってOverall winnerとSponsor Prizeを獲得しました。
OCIの各種PaaSサービスを使ったサーバレスアーキテクチャを組んだので、まとめておきたいと思います。
https://github.com/Contact-IoT-Digital-Signage

今回の記事の内容

OCI API GatewayでFirebase Authと連携して認証を組む方法

環境

OCI https://cloud.oracle.com/

Firebaseのトークンを検証するFunctionsの作成

firebaseのトークン検証にはadmin sdkを使います。
firebaseのconfigファイルjsonを、func.pyと同じディレクトリに突っ込んで、fn deployします。
認証の可否はactiveというパラメータをjsonで返すことで、api gatewayの方でHTTPレスポンスとして返却してくれます。
firebase_adminはrequirements.txtにfirebase-adminと書いておくことでfn deployのときにインストールされます。

import firebase_admin
from firebase_admin import credentials
from firebase_admin import auth
import io
import json
import logging

from fdk import response

cred = credentials.Certificate("firebase_admin_sdk_config.json")
firebase_admin.initialize_app(cred)

def handler(ctx, data: io.BytesIO = None):
    id_token = json.loads(data.getvalue())['data'].get('authorization', '')

    try:
        decoded_token = auth.verify_id_token(id_token)
        uid = decoded_token['uid']

        return json.dumps({
            "active": True
        })
    except:
        return json.dumps({
            "active": False
        })

API Gatewayの設定

Function Argumentsの設定は以下のようにしました。
image.png

また、RouteごとにAuthorizationの設定ができないので、設定ごとにdeploymentを作ることになりそうです。
例えばこれは、認証ありのAPIと認証なしのAPIを、Deploymentでわけた例です。

image.png

まとめ

OCIのGatewaysに認証を設定する方法をまとめました。
OCIにはAWS CognitoのようなID管理サービスがないので、今回の記事のようにFirebaseと組み合わせるのがお手軽かと思います。

質問、ご指摘歓迎します。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?