Firebase Authentication plugin for Ktor framework.

Overview

ktor-server-firebase-auth

Firebase Authentication is a Ktor plugin which verifies requests authorized by a Firebase Auth Id Token.

Usage

// Configure FirebaseAuthentication
install(FirebaseAuthentication) {

    // Sets the FirebaseApp instance to verify tokens.
    // If not specified, default FirebaseApp instance will be used.
    firebaseApp = FirebaseApp.getInstance()

    // Sets the authorization type used in the requests.
    // Default is AuthorizationType.Bearer
    authorizationType = AuthorizationType.Parameter("token")

    // Sets whether to check if the tokens were revoked or if the user is disabled.
    // Defaults to false.
    checkRevoked = true
}

Handle request

HttpStatusCode.InternalServerError FirebaseAuthentication.FailureReason.TokenInvalid -> HttpStatusCode.Unauthorized FirebaseAuthentication.FailureReason.TokenNullOrBlank -> HttpStatusCode.BadRequest null -> throw RuntimeException() // This can never occur, as decodedToken is null. } call.respond(status) } else { call.respond(HttpStatusCode.OK, decodedToken.uid) } } ">
get("/test"){
    val decodedToken = call.getDecodedToken()
    if (decodedToken == null){
        val status = when (call.getFailureReason()){
            FirebaseAuthentication.FailureReason.InvalidFirebaseApp -> HttpStatusCode.InternalServerError
            FirebaseAuthentication.FailureReason.TokenInvalid -> HttpStatusCode.Unauthorized
            FirebaseAuthentication.FailureReason.TokenNullOrBlank -> HttpStatusCode.BadRequest
            null -> throw RuntimeException() // This can never occur, as decodedToken is null.
        }
        call.respond(status)
    } else {
        call.respond(HttpStatusCode.OK, decodedToken.uid)
    }
}

ApplicationCall.getDecodedToken() - Extension function for [ApplicationCall] which gets the decoded token (if a valid token was present, or null) from the attributes. ApplicationCall.getFailureReason() - Extension function for [ApplicationCall] which gets the failure reason if no valid token was found.

Configuration

firebaseApp : FirebaseApp - The FirebaseApp to use to verify id tokens with Firebase servers. If set to null, default instance of FirebaseApp will be used. authorizationType : AutorizationType - The type of authorization used in the requests. Currently supports AuthorizationType.Bearer, AuthorizationType.Parameter, AuthorizationType.Custom. Defaults to AuthorizationType.Bearer. checkRevoked : Boolean - A boolean denoting whether to check if the tokens were revoked or if the user is disabled. Defaults to false.

Contribution

Any contributions and feature requests are welcome.

You might also like...
Implementing JWT token authentication on Android with Clean Architecture
Implementing JWT token authentication on Android with Clean Architecture

Implementando autenticação com token JWT no Android com Clean Architecture Implementar autenticação com Token JWT no Android não é uma tarefa fácil. É

A custom OTP view to enter a code usually used in authentication
A custom OTP view to enter a code usually used in authentication

A custom view to enter a code usually used in authentication. Different types of OTPViews. Easy to use and configure your own view and character of OTP using all the attributes.

It is a repository containing backend structure for Ktor.
It is a repository containing backend structure for Ktor.

Backend Architecture with Ktor + KMongo This project contains, Authentication using Jwt Database Layer (KMongo - Orm for MongoDB) Routing Advanced Rou

Kotlin backend based on the Clean Architecture principles. Ktor, JWT, Exposed, Flyway, KGraphQL/GraphQL generated endpoints, Gradle.
Kotlin backend based on the Clean Architecture principles. Ktor, JWT, Exposed, Flyway, KGraphQL/GraphQL generated endpoints, Gradle.

Kotlin Clean Architecture Backend Kotlin backend based on the Clean Architecture principles. The application is separated into three modules: Domain,

Ktor OpenAPI Spec Generator

Kompendium What is Kompendium Kompendium is intended to be a minimally invasive OpenApi Specification generator for Ktor. Minimally invasive meaning t

sharex image uploader using ktor

ktor-sharex-uploader uploader zdjec napisany w kotlinie przy uzyciu ktor pobierak gotowa jarka jest do pobrania tutaj config apki konfiguracje apki ma

This is a Ktor project to build your own Url shortener
This is a Ktor project to build your own Url shortener

Ktor URL Shortner This project is a implementation for creating Short URL using Ktor + Kotlin + MongoDB Usage It contains two routes if you want to im

SSU u-saint parser with Kotlin-Multiplatform and Ktor.

kusaint Soongsil University(SSU) u-Saint Parser with Kotlin Multiplatform. Prerequisites JVM !!IMPORTANT!! To run kusaint as a library in JVM environm

A simple (and naive) RESTful API made with Ktor, jasync-sql and JWT.

A simple (and naive) RESTful API made with Ktor, jasync-sql and JWT. Route Method Description /account POST Create a new account /account DELETE Delet

Releases(v1.0)
Owner
Manav Tamboli
Self Taught Android Developer
Manav Tamboli
User Authentication ( Compose Ktor Server MongoDB)

Utilising Security in android for Authentication and Authorization for Android I would be using mongo db to perform all basic operations no need for f

Michael Enoma 0 May 2, 2022
Login-and-Signup - Simple Login-and-Signup with authentication using Firebase API

Simple Login-and-Signup with authentication using Firebase API. Log in Sign Up

Indresh Goswami 0 Mar 25, 2022
Integration Testing Kotlin Multiplatform Kata for Kotlin Developers. The main goal is to practice integration testing using Ktor and Ktor Client Mock

This kata is a Kotlin multiplatform version of the kata KataTODOApiClientKotlin of Karumi. We are here to practice integration testing using HTTP stub

Jorge Sánchez Fernández 29 Oct 3, 2022
KTor-Client---Android - The essence of KTor Client for network calls

KTor Client - Android This project encompasses the essence of KTor Client for ne

Mansoor Nisar 2 Jan 18, 2022
A Modern Kotlin-Ktor RESTful API example. Connects to a PostgreSQL database and uses Exposed framework for database operations.

kotlin-ktor-rest-api A Modern Kotlin-Ktor RESTful API example. Connects to a PostgreSQL database and uses Exposed framework for database operations. F

Selim Atasoy 32 Dec 20, 2022
Ktor is an asynchronous framework for creating microservices, web applications and more.

ktor-sample Ktor is an asynchronous framework for creating microservices, web applications and more. Written in Kotlin from the ground up. Application

mohamed tamer 5 Jan 22, 2022
The WeeBe application is a social media-type app built on Ktor framework

The WeeBe application is a social media-type app built on Ktor framework that allows users to exchange various content connected with mental health, motivation, psychology, and improving oneself. Users can share posts with texts, images, videos, and links, as well as discuss the content in the comment section

Perpetio 3 Aug 5, 2022
🍓CookHelper - food social network. The Api and Websocket are based on Ktor framework. Dependency injection with Koin library.

CookHelper [ ?? Work in Progress ?? ] CookHelper is a cross-platform application that will allow you to cook a delicious dish from an existing recipe

Arthur 11 Nov 9, 2022
Plugin to integrate EventStoreDB into Ktor-Server!

Ktor Plugin EventStoreDB EventStoreDB is an open-source database technology that stores your critical data in streams of immutable events. It was buil

null 7 Sep 1, 2022
🪟 Pluggable Ktor plugin to implement Sentry for error handling and request contexts

?? Ktor Plugin for Sentry Pluggable Ktor plugin to implement Sentry for error handling and request contexts. What is this library? This basically impl

Noel 3 Dec 6, 2022