Skip to content

Commit

Permalink
fix firebase authentication
Browse files Browse the repository at this point in the history
  • Loading branch information
febrihidayan committed Feb 7, 2024
1 parent f394f54 commit a2883fb
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 62 deletions.
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,6 @@ tmp
.idea
temp-assets/*
!temp-assets/README.md
vendor/*
vendor/*
data/config/*.*
!data/config/*.example
13 changes: 13 additions & 0 deletions data/config/firebase_key.json.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"type": "service_account",
"project_id": "",
"private_key_id": "",
"private_key": "",
"client_email": "",
"client_id": "",
"auth_uri": "https://accounts.google.com/o/oauth2/auth",
"token_uri": "https://oauth2.googleapis.com/token",
"auth_provider_x509_cert_url": "https://www.googleapis.com/oauth2/v1/certs",
"client_x509_cert_url": "",
"universe_domain": "googleapis.com"
}
12 changes: 1 addition & 11 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -111,17 +111,7 @@ services:
MONGODB_NAME: arch_notification
MONGODB_HOST: 'mongo'
MONGODB_PORT: '27017'
GOOGLE_FIREBASE_TYPE: "service_account"
GOOGLE_FIREBASE_PROJECT_ID: ""
GOOGLE_FIREBASE_PRIVATE_KEY_ID: ""
GOOGLE_FIREBASE_PRIVATE_KEY: ""
GOOGLE_FIREBASE_CLIENT_EMAIL: ""
GOOGLE_FIREBASE_CLIENT_ID: ""
GOOGLE_FIREBASE_AUTH_URI: "https://accounts.google.com/o/oauth2/auth"
GOOGLE_FIREBASE_TOKEN_URI: "https://oauth2.googleapis.com/token"
GOOGLE_FIREBASE_AUTH_PROVIDER_CERT_URL: "https://www.googleapis.com/oauth2/v1/certs"
GOOGLE_FIREBASE_CLIENT_CERT_URL: ""
GOOGLE_FIREBASE_UNIVERSE_DOMAIN: "googleapis.com"
GOOGLE_FIREBASE_PATH: "data/config/firebase_key.json"
MAILGUN_FROM_DOMAIN: ""
MAILGUN_FROM_NAME: ""
MAILGUN_DOMAIN: ""
Expand Down
12 changes: 1 addition & 11 deletions env/notification/.env.example
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,7 @@ MONGODB_NAME="arch_notification"
MONGODB_HOST="localhost"
MONGODB_PORT="27017"

GOOGLE_FIREBASE_TYPE="service_account"
GOOGLE_FIREBASE_PROJECT_ID=""
GOOGLE_FIREBASE_PRIVATE_KEY_ID=""
GOOGLE_FIREBASE_PRIVATE_KEY=""
GOOGLE_FIREBASE_CLIENT_EMAIL=""
GOOGLE_FIREBASE_CLIENT_ID=""
GOOGLE_FIREBASE_AUTH_URI="https://accounts.google.com/o/oauth2/auth"
GOOGLE_FIREBASE_TOKEN_URI="https://oauth2.googleapis.com/token"
GOOGLE_FIREBASE_AUTH_PROVIDER_CERT_URL="https://www.googleapis.com/oauth2/v1/certs"
GOOGLE_FIREBASE_CLIENT_CERT_URL=""
GOOGLE_FIREBASE_UNIVERSE_DOMAIN="googleapis.com"
GOOGLE_FIREBASE_PATH="data/config/firebase_key.json"

MAILGUN_FROM_DOMAIN=""
MAILGUN_FROM_NAME=""
Expand Down
24 changes: 2 additions & 22 deletions services/notification/internal/config/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,17 +21,7 @@ type GrpcClient struct {
}

type FirebaseGoogle struct {
Type string
ProjectId string
PrivateKeyId string
PrivateKey string
ClientEmail string
ClientId string
AuthUri string
TokenUri string
AuthProviderCertUrl string
ClientCertUrl string
UniverseDomain string
Path string
}

type Mailgun struct {
Expand All @@ -50,17 +40,7 @@ func Notification() *NotificationConfig {
User: os.Getenv("RPC_USER"),
},
FirebaseGoogleService: &FirebaseGoogle{
Type: os.Getenv("GOOGLE_FIREBASE_TYPE"),
ProjectId: os.Getenv("GOOGLE_FIREBASE_PROJECT_ID"),
PrivateKeyId: os.Getenv("GOOGLE_FIREBASE_PRIVATE_KEY_ID"),
PrivateKey: os.Getenv("GOOGLE_FIREBASE_PRIVATE_KEY"),
ClientEmail: os.Getenv("GOOGLE_FIREBASE_CLIENT_EMAIL"),
ClientId: os.Getenv("GOOGLE_FIREBASE_CLIENT_ID"),
AuthUri: os.Getenv("GOOGLE_FIREBASE_AUTH_URI"),
TokenUri: os.Getenv("GOOGLE_FIREBASE_TOKEN_URI"),
AuthProviderCertUrl: os.Getenv("GOOGLE_FIREBASE_AUTH_PROVIDER_CERT_URL"),
ClientCertUrl: os.Getenv("GOOGLE_FIREBASE_CLIENT_CERT_URL"),
UniverseDomain: os.Getenv("GOOGLE_FIREBASE_UNIVERSE_DOMAIN"),
Path: os.Getenv("GOOGLE_FIREBASE_PATH"),
},
Mailgun: &Mailgun{
MailFromDomain: os.Getenv("MAILGUN_FROM_DOMAIN"),
Expand Down
21 changes: 4 additions & 17 deletions services/notification/internal/services/firebase_google.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package services

import (
"context"
"encoding/json"
"path/filepath"
"strings"

firebase "firebase.google.com/go"
Expand All @@ -17,26 +17,13 @@ type FirebaseGoogleService struct {
}

func NewFcmGoogleService(cfg *config.FirebaseGoogle) (*FirebaseGoogleService, error) {
config := map[string]string{
"type": cfg.Type,
"project_id": cfg.ProjectId,
"private_key_id": cfg.PrivateKeyId,
"private_key": cfg.PrivateKey,
"client_email": cfg.ClientEmail,
"client_id": cfg.ClientId,
"auth_uri": cfg.AuthUri,
"token_uri": cfg.TokenUri,
"auth_provider_x509_cert_url": cfg.AuthProviderCertUrl,
"client_x509_cert_url": cfg.ClientCertUrl,
"universe_domain": cfg.UniverseDomain,
}

configByte, err := json.Marshal(config)
path, err := filepath.Abs(cfg.Path)
if err != nil {
return nil, err
}

opt := option.WithCredentialsJSON(configByte)
opt := option.WithCredentialsFile(path)

app, err := firebase.NewApp(context.Background(), nil, opt)
if err != nil {
return nil, err
Expand Down

0 comments on commit a2883fb

Please sign in to comment.