Skip to content

Commit

Permalink
Release 1.0.2 (#23)
Browse files Browse the repository at this point in the history
* Enabling the use of App Engine's SignBytes, ServiceAccount and PublicCertificates (#18)

* Release 1.0.1 (#16)

* Using the ClientOptions provided at App initialization to create the … (#12)

* Using the ClientOptions provided at App initialization to create the HTTPClient in auth package.

* Fixed context import

* Updated test case

* Fixing a test failure; Calling transport.NewHTTPClient() only when ctx and opts are available to avoid an unnecessary default credentials lookup.

* Passing a non-nil context to AuthConfig during testing; Replacing Print+Exit calls with log.Fatal() (#13)

* Bumped version to 1.0.1 (#15)

* adding native app engine support via build tags

* missing files

* adding ability to use standard signer locally with GAE

* allowing for std signer on GAE production env, fixing opts check

* fixing env var name

* removing test log

* giving the key source the same treatment as the signer

* missing files

* removing unneeded build tag

* code review feedback

* missing file

* adjusting constructor per review feedback

* file rename per feedback

* reverting more changes

* review feedback, fixing test

* Documentation update (#19)

* Release 1.0.1 (#16)

* Using the ClientOptions provided at App initialization to create the … (#12)

* Using the ClientOptions provided at App initialization to create the HTTPClient in auth package.

* Fixed context import

* Updated test case

* Fixing a test failure; Calling transport.NewHTTPClient() only when ctx and opts are available to avoid an unnecessary default credentials lookup.

* Passing a non-nil context to AuthConfig during testing; Replacing Print+Exit calls with log.Fatal() (#13)

* Bumped version to 1.0.1 (#15)

* Removed unused package variable; Updated godocs

* Minor refactoring of test code (#20)

* Release 1.0.1 (#16)

* Using the ClientOptions provided at App initialization to create the … (#12)

* Using the ClientOptions provided at App initialization to create the HTTPClient in auth package.

* Fixed context import

* Updated test case

* Fixing a test failure; Calling transport.NewHTTPClient() only when ctx and opts are available to avoid an unnecessary default credentials lookup.

* Passing a non-nil context to AuthConfig during testing; Replacing Print+Exit calls with log.Fatal() (#13)

* Bumped version to 1.0.1 (#15)

* Moved integration/auth_test.go to its own package. Moved fileKeySource to auth_test.go

* Testing for iss and sub headers of custom tokens

* Bumped version to 1.0.2 (#21)

* Release 1.0.1 (#16)

* Using the ClientOptions provided at App initialization to create the … (#12)

* Using the ClientOptions provided at App initialization to create the HTTPClient in auth package.

* Fixed context import

* Updated test case

* Fixing a test failure; Calling transport.NewHTTPClient() only when ctx and opts are available to avoid an unnecessary default credentials lookup.

* Passing a non-nil context to AuthConfig during testing; Replacing Print+Exit calls with log.Fatal() (#13)

* Bumped version to 1.0.1 (#15)

* Bumped version to 1.0.2wq
  • Loading branch information
hiranya911 committed Sep 13, 2017
1 parent a787b7a commit 027e651
Show file tree
Hide file tree
Showing 9 changed files with 320 additions and 142 deletions.
84 changes: 48 additions & 36 deletions auth/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,14 @@
package auth

import (
"crypto/rsa"
"crypto/x509"
"encoding/json"
"encoding/pem"
"errors"
"fmt"
"strings"

"crypto/rsa"
"crypto/x509"

"firebase.google.com/go/internal"
)

Expand Down Expand Up @@ -62,45 +61,60 @@ type Token struct {
type Client struct {
ks keySource
projectID string
email string
pk *rsa.PrivateKey
snr signer
}

type signer interface {
Email() (string, error)
Sign(b []byte) ([]byte, error)
}

// NewClient creates a new instance of the Firebase Auth Client.
//
// This function can only be invoked from within the SDK. Client applications should access the
// the Auth service through firebase.App.
func NewClient(c *internal.AuthConfig) (*Client, error) {
var (
err error
email string
pk *rsa.PrivateKey
)
if c.Creds != nil && len(c.Creds.JSON) > 0 {
var svcAcct struct {
ClientEmail string `json:"client_email"`
PrivateKey string `json:"private_key"`
}
if err := json.Unmarshal(c.Creds.JSON, &svcAcct); err != nil {
return nil, err
}
if svcAcct.PrivateKey != "" {
pk, err = parseKey(svcAcct.PrivateKey)
if err != nil {
return nil, err
}
}
email = svcAcct.ClientEmail
}
var snr signer
if email != "" && pk != nil {
snr = serviceAcctSigner{email: email, pk: pk}
} else {
snr, err = newSigner(c.Ctx)
if err != nil {
return nil, err
}
}

ks, err := newHTTPKeySource(c.Ctx, googleCertURL, c.Opts...)
if err != nil {
return nil, err
}

client := &Client{
return &Client{
ks: ks,
projectID: c.ProjectID,
}
if c.Creds == nil || len(c.Creds.JSON) == 0 {
return client, nil
}

var svcAcct struct {
ClientEmail string `json:"client_email"`
PrivateKey string `json:"private_key"`
}
if err := json.Unmarshal(c.Creds.JSON, &svcAcct); err != nil {
return nil, err
}

if svcAcct.PrivateKey != "" {
pk, err := parseKey(svcAcct.PrivateKey)
if err != nil {
return nil, err
}
client.pk = pk
}
client.email = svcAcct.ClientEmail
return client, nil
snr: snr,
}, nil
}

// CustomToken creates a signed custom authentication token with the specified user ID. The resulting
Expand All @@ -114,11 +128,9 @@ func (c *Client) CustomToken(uid string) (string, error) {
// CustomTokenWithClaims is similar to CustomToken, but in addition to the user ID, it also encodes
// all the key-value pairs in the provided map as claims in the resulting JWT.
func (c *Client) CustomTokenWithClaims(uid string, devClaims map[string]interface{}) (string, error) {
if c.email == "" {
return "", errors.New("service account email not available")
}
if c.pk == nil {
return "", errors.New("private key not available")
iss, err := c.snr.Email()
if err != nil {
return "", err
}

if len(uid) == 0 || len(uid) > 128 {
Expand All @@ -139,15 +151,15 @@ func (c *Client) CustomTokenWithClaims(uid string, devClaims map[string]interfac

now := clk.Now().Unix()
payload := &customToken{
Iss: c.email,
Sub: c.email,
Iss: iss,
Sub: iss,
Aud: firebaseAudience,
UID: uid,
Iat: now,
Exp: now + tokenExpSeconds,
Claims: devClaims,
}
return encodeToken(defaultHeader(), payload, c.pk)
return encodeToken(c.snr, defaultHeader(), payload)
}

// VerifyIDToken verifies the signature and payload of the provided ID token.
Expand Down
40 changes: 40 additions & 0 deletions auth/auth_appengine.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
// +build appengine

// Copyright 2017 Google Inc. All Rights Reserved.
//
// 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.

package auth

import (
"golang.org/x/net/context"

"google.golang.org/appengine"
)

type aeSigner struct {
ctx context.Context
}

func newSigner(ctx context.Context) (signer, error) {
return aeSigner{ctx}, nil
}

func (s aeSigner) Email() (string, error) {
return appengine.ServiceAccount(s.ctx)
}

func (s aeSigner) Sign(ss []byte) ([]byte, error) {
_, sig, err := appengine.SignBytes(s.ctx, ss)
return sig, err
}
23 changes: 23 additions & 0 deletions auth/auth_std.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// +build !appengine

// Copyright 2017 Google Inc. All Rights Reserved.
//
// 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.

package auth

import "context"

func newSigner(ctx context.Context) (signer, error) {
return serviceAcctSigner{}, nil
}
Loading

0 comments on commit 027e651

Please sign in to comment.