Skip to content

Instantly share code, notes, and snippets.

@aj0strow
Created August 17, 2017 20:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aj0strow/e3d95171c6df724fdf55e031373e8426 to your computer and use it in GitHub Desktop.
Save aj0strow/e3d95171c6df724fdf55e031373e8426 to your computer and use it in GitHub Desktop.
Firebase Custom Tokens
package firebase
import (
"github.com/knq/jwt"
"github.com/knq/jwt/gserviceaccount"
"time"
)
// Firebase JWT Claims
type Claims struct {
Iss string `json:"iss"`
Sub string `json:"sub"`
Aud string `json:"aud"`
Iat int64 `json:"iat"`
Exp int64 `json:"exp"`
Uid string `json:"uid"`
}
// Firebase App
type App struct {
GServiceAccount *gserviceaccount.GServiceAccount
}
// Issue custom JWT token used for client login.
func (app *App) CustomToken(uid string) ([]byte, error) {
signer, err := app.GServiceAccount.Signer()
if err != nil {
return nil, err
}
now := time.Now().Unix()
return jwt.Encode(jwt.RS256, signer, Claims{
Iss: app.GServiceAccount.ClientEmail,
Sub: app.GServiceAccount.ClientEmail,
Aud: "https://identitytoolkit.googleapis.com/google.identity.identitytoolkit.v1.IdentityToolkit",
Iat: now,
Exp: now + 60*60,
Uid: uid,
})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment