Categorygithub.com/retrorabbit/firebase-server-sdk-go
modulepackage
1.0.2
Repository: https://github.com/retrorabbit/firebase-server-sdk-go.git
Documentation: pkg.go.dev

# README

Firebase Server SDK for Golang

This is the Server SDK written in Golang for the 2016 newly announced Firebase suite of services.

Note that this is not an official SDK written by Google/Firebase. Firebase only offers the Server SDK in Java and Node.js. This is simply an attempt to implement the Firebase Server SDK by reverse engineering the official ones. If you decide to use this SDK, be warned that you may need to migrate at some point in the future when Google decides to release an official go SDK.

This SDK, like its Java and Node counterparts, supports the following functions needed on the application server:

  • Authentication
    • Create custom tokens suitable for integrating custom auth systems with Firebase apps.
    • Verify ID tokens, which are used to pass the signed-in user from a client app to a backend server.
  • Realtime Database
    • This is a lot more involved so stay tuned.
    • For now you can use firego or Go Firebase, which are based on the Firebase REST API. These libraries are not real-time but they will allow you to read from and write to the Firebase database. Note that if you use firego, I recommend using my forked branch, which allows you to use the application default token source (which refreshes itself).
  • Cloud Messaging (FCM)
    • This is not offered even in the official Server SDKs, but it would be convenient to include this feature.
    • If you wish to use a separate client library for this feature, you can try wuman/go-gcm or google/go-gcm.

Installation

Install the package with go:

go get github.com/wuman/firebase-server-sdk-go

Import the package to your go file:

import (
	firebase "github.com/wuman/firebase-server-sdk-go"
)

Documentation

You can find documentation on godoc.org.

Initialize Firebase

Once you have created a Firebase console project and downloaded a JSON file with your service account credentials, you can initialize the SDK with this code snippet:

firebase.InitializeApp(&firebase.Options{
	ServiceAccountPath: "path/to/serviceAccountCredentials.json",
})

Create Custom Tokens

To create a custom token, pass the unique user ID used by your auth system to the CreateCustomToken() method:

auth, _ := firebase.GetAuth()
token, err := auth.CreateCustomToken(userId, nil)

You can also optionally specify additional claims to be included in the custom token. These claims will be available in the auth/request.auth objects in your Security Rules. For example:

auth, _ := firebase.GetAuth()
developerClaims = make(firebase.Claims)
developerClaims["premium_account"] = true
token, err := auth.CreateCustomToken(userId, &developerClaims)

Verify ID Tokens

To verify and decode an ID Token with the SDK, pass the ID Token to the VerifyIDToken method. If the ID Token is not expired and is properly signed, the method decodes the ID Token.

auth, _ := firebase.GetAuth()
decodedToken, err := auth.VerifyIDToken(idTokenString)
if err == nil {
	uid, found := decodedToken.Uid()
}

To-Do List

  • add travis CI
  • add sample
  • remove dependency on JWT library jose to keep the SDK lean (low priority)

Developed By

LICENSE

Copyright 2016 David Wu

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

# Functions

GetApp retrieves the default instance of the App, creating it if necessary.
GetAppWithName retrieves an instance of the App with a given name, creating it if necessary.
GetAuth gets the Auth instance for the default App.
GetAuthWithApp gets an instance of Auth for a specific App.
InitializeApp initializes the default App instance.
InitializeAppWithName initializes an App with a unique given name.

# Variables

AuthErrEmailAlreadyExists represents the default api error that the provided email is already in use by an existing user.
AuthErrInsufficientPermission represents the default api error that the credential used to initialize the SDK has insufficient permission to access the requested Authentication resource.
AuthErrInternalError represents the default api error that the Authentication server encountered an unexpected error while trying to process the request.
AuthErrInvalidArgument represents the default api error that an invalid argument was provided to an Authentication method.
AuthErrInvalidCredential represents the default api error that the credential used to authenticate the Admin SDKs cannot be used to perform the desired action.
AuthErrInvalidDisabledField represents the default api error that the provided value for the disabled user property is invalid.
AuthErrInvalidDisplayName represents the default api error that the provided value for the displayName user property is invalid.
AuthErrInvalidEmail represents the default api error that the provided value for the email user property is invalid.
AuthErrInvalidEmailVerified represents the default api error that the provided value for the emailVerified user property is invalid.
AuthErrInvalidPassword represents the default api error that the provided value for the password user property is invalid.
AuthErrInvalidPassword represents the default api error that the provided value for the password user property is invalid.
AuthErrInvalidPhotoURL represents the default api error that the provided value for the photoURL user property is invalid.
AuthErrInvalidUID represents the default api error that the provided uid is invalid.
AuthErrMissingUID represents the default api error that a uid identifier is required for the current operation.
AuthErrOperationNotAllowed represents the default api error that the provided sign-in provider is disabled for your Firebase project.
AuthErrProjectNotFound represents the default api error that no Firebase project was found for the credential used to initialize the SDK.
AuthErrUIDAlreadyExists represents the default api error that the provided uid is already in use by an existing user.
AuthErrUserNotFound represents the default api error that there is no existing user record corresponding to the provided identifier.
No description provided by the author

# Structs

APIError defines the data model of Firebase API errors.
App is the entry point of the SDK.
Auth is the entry point for all server-side Firebase Authentication actions.
Certificates holds a collection of public certificates that are fetched from a given URL.
Clock returns the current system time.
GoogleServiceAccountCredential is the credential for a GCP Service Account.
MockClock can be used to mock current time during tests.
Options is storage for configurable Firebase options.
Token represents a decoded Firebase ID token.
UserInfo defines the data model for Firebase interface representing a user's info from a third-party identity provider such as Google or Facebook.
UserMetadata defines the data model for Firebase interface representing a user's metadata.
UserRecord defines the data model for Firebase interface representing a user.

# Interfaces

Clock is used to query the current local time.

# Type aliases

Claims to be stored in a custom token (and made available to security rules in Database, Storage, etc.).
UserProperties defines the input user properties in a create or edit user API.