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.
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"
)
You can find documentation on godoc.org.
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",
})
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)
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()
}
- add travis CI
- add sample
- remove dependency on JWT library jose to keep the SDK lean (low priority)
- David Wu - [email protected] - http://blog.wu-man.com
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.