-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feature: auth 用のエンドポイントを追加しました * fix: checkout を追加した * fix: これでどうですか??
- Loading branch information
1 parent
c397749
commit 3fddd03
Showing
5 changed files
with
133 additions
and
9 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package firebase | ||
|
||
import ( | ||
"context" | ||
"log" | ||
"os" | ||
|
||
firebase "firebase.google.com/go" | ||
"google.golang.org/api/option" | ||
) | ||
|
||
var firebaseApp *firebase.App | ||
|
||
func init() { | ||
opt := option.WithCredentialsJSON([]byte(os.Getenv("FIREBASE_KEYFILE_JSON"))) | ||
var err error | ||
firebaseApp, err = firebase.NewApp(context.Background(), nil, opt) | ||
if err != nil { | ||
log.Fatalf("firebase app の初期化に失敗しました: %v", err) | ||
} | ||
} | ||
|
||
func GetFirebaseApp() *firebase.App { | ||
return firebaseApp | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package lib | ||
|
||
import ( | ||
"net/http" | ||
"strings" | ||
|
||
"golang.org/x/xerrors" | ||
) | ||
|
||
func GetAuthorizationBarerTokenFromHeader(header http.Header) (string, error) { | ||
authHeader := header.Get("Authorization") | ||
if authHeader == "" { | ||
return "", xerrors.Errorf("Authorization ヘッダーが設定されていません。") | ||
} | ||
|
||
splitToken := strings.Split(authHeader, "Bearer ") | ||
if len(splitToken) != 2 { | ||
return "", xerrors.Errorf("token の形式が不正です。") | ||
} | ||
|
||
barerToken := splitToken[1] | ||
|
||
return barerToken, nil | ||
} |
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
package middleware | ||
|
||
import ( | ||
"fmt" | ||
"log" | ||
"net/http" | ||
|
||
"github.com/labstack/echo/v4" | ||
|
||
FirebaseInfrastructure "github.com/recordex/backend/infrastructure/firebase" | ||
"github.com/recordex/backend/lib" | ||
) | ||
|
||
func FirebaseAuth() echo.MiddlewareFunc { | ||
return func(next echo.HandlerFunc) echo.HandlerFunc { | ||
return func(c echo.Context) error { | ||
ctx := c.Request().Context() | ||
firebaseApp := FirebaseInfrastructure.GetFirebaseApp() | ||
authClient, err := firebaseApp.Auth(ctx) | ||
if err != nil { | ||
fmt.Println("firebaseApp.Auth(ctx) でエラーが発生しました。") | ||
return c.JSON(http.StatusInternalServerError, map[string]string{ | ||
"message": err.Error(), | ||
}) | ||
} | ||
|
||
barerToken, err := lib.GetAuthorizationBarerTokenFromHeader(c.Request().Header) | ||
if err != nil { | ||
return c.JSON(http.StatusBadRequest, map[string]string{ | ||
"message": err.Error(), | ||
}) | ||
} | ||
|
||
token, err := authClient.VerifyIDToken(ctx, barerToken) | ||
if err != nil { | ||
return c.JSON(http.StatusUnauthorized, map[string]string{ | ||
"message": err.Error(), | ||
}) | ||
} | ||
|
||
log.Printf("idToken の検証に成功しました。uid -> %s", token.UID) | ||
c.Set("userId", token.UID) | ||
return next(c) | ||
} | ||
} | ||
} |