-
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 #38 from kynmh69/35-feature-add-key-auth
35 feature add key auth
- Loading branch information
Showing
8 changed files
with
142 additions
and
25 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
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,26 @@ | ||
package middleware | ||
|
||
import ( | ||
"github.com/gin-gonic/gin" | ||
"github.com/kynmh69/go-ja-holidays/logging" | ||
"github.com/kynmh69/go-ja-holidays/model" | ||
"net/http" | ||
) | ||
|
||
func Auth(c *gin.Context) { | ||
logger := logging.GetLogger() | ||
// get api key from request header | ||
key := c.GetHeader("X-API-KEY") | ||
if key == "" { | ||
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"error": "API key is required."}) | ||
return | ||
} | ||
apiKey, err := model.GetApiKey(key) | ||
if err != nil { | ||
logger.Warnln("API key is invalid. ", key) | ||
c.AbortWithStatusJSON(http.StatusForbidden, gin.H{"error": "API key is invalid."}) | ||
return | ||
} | ||
logger.Debugln("API key is valid. ", apiKey.Id) | ||
c.Next() | ||
} |
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,79 @@ | ||
package middleware | ||
|
||
import ( | ||
"github.com/gin-gonic/gin" | ||
"github.com/google/uuid" | ||
"github.com/kynmh69/go-ja-holidays/database" | ||
"github.com/kynmh69/go-ja-holidays/logging" | ||
"github.com/kynmh69/go-ja-holidays/model" | ||
"gorm.io/gorm" | ||
"net/http" | ||
"net/http/httptest" | ||
"os" | ||
"testing" | ||
) | ||
|
||
func TestMain(m *testing.M) { | ||
_ = os.Setenv("PSQL_HOSTNAME", "localhost") | ||
_ = os.Setenv("DATABASE", "unittest") | ||
logging.LoggerInitialize() | ||
database.ConnectDatabase() | ||
|
||
defer tearDown() | ||
|
||
db := database.GetDbConnection() | ||
if err := db.AutoMigrate( | ||
&model.ApiKey{}, | ||
&model.HolidayData{}, | ||
); err != nil { | ||
logging.GetLogger().Panicln(err) | ||
} | ||
if code := m.Run(); code > 0 { | ||
logging.GetLogger().Panicln("Test failed with code ", code) | ||
} | ||
logging.GetLogger().Infoln("Test passed") | ||
} | ||
|
||
func TestAuth(t *testing.T) { | ||
r := gin.Default() | ||
u, _ := uuid.NewUUID() | ||
var apiKey model.ApiKey | ||
apiKeyError := model.ApiKey{Key: u} | ||
db := database.GetDbConnection() | ||
db.Create(&apiKey) | ||
tests := []struct { | ||
name string | ||
apiKey model.ApiKey | ||
statusCode int | ||
}{ | ||
{ | ||
name: "Test Auth", | ||
apiKey: apiKey, | ||
statusCode: http.StatusOK, | ||
}, | ||
{ | ||
name: "Test Auth Error", | ||
apiKey: apiKeyError, | ||
statusCode: http.StatusOK, | ||
}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
req := httptest.NewRequest(http.MethodGet, "/", nil) | ||
req.Header.Set("X-API-KEY", tt.apiKey.Key.String()) | ||
w := httptest.NewRecorder() | ||
ctx := gin.CreateTestContextOnly(w, r) | ||
ctx.Request = req | ||
Auth(ctx) | ||
if tt.statusCode != w.Code { | ||
t.Errorf("expected %d but got %d", tt.statusCode, w.Code) | ||
} | ||
}) | ||
} | ||
} | ||
|
||
func tearDown() { | ||
db := database.GetDbConnection() | ||
db.Session(&gorm.Session{AllowGlobalUpdate: true}). | ||
Delete(&model.ApiKey{}) | ||
} |
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