Created
August 17, 2017 20:16
-
-
Save aj0strow/e3d95171c6df724fdf55e031373e8426 to your computer and use it in GitHub Desktop.
Firebase Custom Tokens
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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