-
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.
Merge pull request #2 from ryichk/add-todo-list-api
Todoリストを取得するAPIを追加
- Loading branch information
Showing
11 changed files
with
201 additions
and
13 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
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,31 @@ | ||
package handler | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/jackc/pgx/v5/pgtype" | ||
"github.com/jackc/pgx/v5/pgxpool" | ||
"github.com/labstack/echo/v4" | ||
) | ||
|
||
type UserInfo struct { | ||
Conn *pgxpool.Conn | ||
UserID pgtype.UUID | ||
} | ||
|
||
func (h *Handler) AcquireConnection(ctx context.Context, c echo.Context) (*UserInfo, error) { | ||
userID, err := UserIDByContext(c) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
conn, err := h.queries.AcquireConnection(ctx, h.pool) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return &UserInfo{ | ||
Conn: conn, | ||
UserID: userID, | ||
}, 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,11 @@ | ||
package handler | ||
|
||
import ( | ||
"net/http" | ||
|
||
"github.com/labstack/echo/v4" | ||
) | ||
|
||
func (h *Handler) Hello(c echo.Context) error { | ||
return c.String(http.StatusOK, "Hello!") | ||
} |
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,22 @@ | ||
package handler | ||
|
||
import ( | ||
"errors" | ||
|
||
"github.com/jackc/pgx/v5/pgtype" | ||
"github.com/labstack/echo/v4" | ||
) | ||
|
||
func UserIDByContext(c echo.Context) (pgtype.UUID, error) { | ||
userIDStr, ok := c.Get("userID").(string) | ||
if !ok { | ||
return pgtype.UUID{}, errors.New("user ID is not a string") | ||
} | ||
|
||
var userID pgtype.UUID | ||
if err := userID.Scan(userIDStr); err != nil { | ||
return pgtype.UUID{}, err | ||
} | ||
|
||
return userID, 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,24 @@ | ||
package handler | ||
|
||
// func (h *Handler) ListTodos(c echo.Context) error { | ||
// rc := c.Request().Context() | ||
import ( | ||
"net/http" | ||
|
||
// todos, err := h.queries.ListTodos(rc) | ||
// } | ||
"github.com/labstack/echo/v4" | ||
) | ||
|
||
func (h *Handler) ListTodos(c echo.Context) error { | ||
ctx := c.Request().Context() | ||
|
||
userInfo, err := h.AcquireConnection(ctx, c) | ||
if err != nil { | ||
return echo.NewHTTPError(http.StatusInternalServerError, err.Error()) | ||
} | ||
defer userInfo.Conn.Release() | ||
|
||
todos, err := h.queries.ListTodos(ctx, userInfo.Conn) | ||
if err != nil { | ||
return echo.NewHTTPError(http.StatusInternalServerError, err.Error()) | ||
} | ||
|
||
return c.JSON(http.StatusOK, todos) | ||
} |
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,16 @@ | ||
package model | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/jackc/pgx/v5/pgxpool" | ||
) | ||
|
||
func (q *Queries) AcquireConnection(ctx context.Context, pool *pgxpool.Pool) (*pgxpool.Conn, error) { | ||
conn, err := pool.Acquire(ctx) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return conn, 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
package server | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
"log" | ||
"net/http" | ||
"os" | ||
"time" | ||
|
||
"github.com/MicahParks/keyfunc/v3" | ||
"github.com/golang-jwt/jwt/v5" | ||
"github.com/labstack/echo/v4" | ||
) | ||
|
||
func AuthMiddleware() echo.MiddlewareFunc { | ||
return func(next echo.HandlerFunc) echo.HandlerFunc { | ||
return func(c echo.Context) error { | ||
jwtBase64 := extractJWTFromHeader(c.Request()) | ||
if jwtBase64 == "" { | ||
return echo.NewHTTPError(http.StatusUnauthorized, "JWT is required") | ||
} | ||
|
||
claims, err := validateJWT(jwtBase64) | ||
if err != nil { | ||
return echo.NewHTTPError(http.StatusUnauthorized, err) | ||
} | ||
c.Set("userID", claims.Subject) | ||
|
||
return next(c) | ||
} | ||
} | ||
} | ||
|
||
type CustomClaims struct { | ||
jwt.RegisteredClaims | ||
} | ||
|
||
func validateJWT(jwtBase64 string) (*CustomClaims, error) { | ||
regionID := os.Getenv("AWS_REGION") | ||
userPoolID := os.Getenv("COGNITO_USER_POOL_ID") | ||
|
||
jwksURL := fmt.Sprintf("https://cognito-idp.%s.amazonaws.com/%s/.well-known/jwks.json", regionID, userPoolID) | ||
jwks, err := keyfunc.NewDefault([]string{jwksURL}) | ||
if err != nil { | ||
log.Printf("Failed to create JWK Set from resource at the given URL.\nError: %v", err) | ||
return nil, errors.New("Failed to create JWT Set") | ||
} | ||
|
||
issuer := fmt.Sprintf("https://cognito-idp.%s.amazonaws.com/%s", regionID, userPoolID) | ||
token, err := jwt.ParseWithClaims(jwtBase64, &CustomClaims{}, jwks.Keyfunc, jwt.WithExpirationRequired(), jwt.WithIssuer(issuer)) | ||
if err != nil { | ||
log.Printf("Failed to parse the JWT.\nError: %v", err) | ||
return nil, errors.New("Failed to parse the JWT") | ||
} | ||
if !token.Valid { | ||
log.Println("The token is invalid.") | ||
return nil, errors.New("The token is invalid") | ||
} | ||
claims, ok := token.Claims.(*CustomClaims) | ||
if !ok { | ||
log.Println("The token has invalid claims.") | ||
return nil, errors.New("The token has invalid claims") | ||
} | ||
if claims.ExpiresAt != nil && claims.ExpiresAt.Before(time.Now()) { | ||
log.Println("The token has expired.") | ||
return nil, errors.New("The token has expired") | ||
} | ||
if claims.NotBefore != nil && claims.NotBefore.After(time.Now()) { | ||
log.Println("The token is not valid yet.") | ||
return nil, errors.New("The token is not valid yet") | ||
} | ||
|
||
return claims, nil | ||
} | ||
|
||
func extractJWTFromHeader(r *http.Request) string { | ||
authHeader := r.Header.Get("Authorization") | ||
if len(authHeader) > 7 && authHeader[:7] == "Bearer " { | ||
return authHeader[7:] | ||
} | ||
return "" | ||
} |
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