Skip to content

Commit

Permalink
🩹 jwtTokenKey as env var
Browse files Browse the repository at this point in the history
  • Loading branch information
ras0q committed Sep 24, 2023
1 parent aeaff4c commit cbbdbed
Show file tree
Hide file tree
Showing 7 changed files with 17 additions and 7 deletions.
1 change: 1 addition & 0 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ WEBHOOK_SECRET=
ACTIVITY_CHANNEL_ID=
DAILY_CHANNEL_ID=
TOKEN_KEY=
JWT_TOKEN_KEY=
KNOQ_VERSION=
KNOQ_REVISION=
DEVELOPMENT=
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ knoQ の全ての機能を動作させるためには、追加の情報が必要
| DAILY_CHANNEL_ID | 環境変数 | | Bot が毎日定時に投稿する先のチャンネル |
| ACTIVITY_CHANNEL_ID | 環境変数 | | Bot が都度送信するチャンネル |
| TOKEN_KEY | 環境変数 | `random32wordsXXXXXXXXXXXXXXXXXXX` | Token を暗号化する。長さ 32 文字のランダム文字列。存在しない場合はエラー。 |
| JWT_TOKEN_KEY | 環境変数 | `random_strings` | JWT の Token を暗号化する。存在しない場合はエラー。 |
| KNOQ_VERSION | 環境変数 | UNKNOWN | knoQ のバージョン (github actions でイメージ作成時に指定) |
| KNOQ_REVISION | 環境変数 | UNKNOWN | git の sha1 (github actions でイメージ作成時に指定) |
| DEVELOPMENT | 環境変数 | | 開発時かどうか |
Expand Down
2 changes: 1 addition & 1 deletion docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ version: "3"
networks:
default:


services:
knoq:
build:
Expand All @@ -26,6 +25,7 @@ services:
DAILY_CHANNEL_ID: ${DAILY_CHANNEL_ID}
ACTIVITY_CHANNEL_ID: ${ACTIVITY_CHANNEL_ID}
TOKEN_KEY: ${TOKEN_KEY:-random32wordsXXXXXXXXXXXXXXXXXXX}
JWT_TOKEN_KEY: ${JWT_TOKEN_KEY:-jwt_token_key}
KNOQ_VERSION: ${KNOQ_VERSION:-dev}
DEVELOPMENT: true
GORM_LOG_LEVEL: info
Expand Down
13 changes: 11 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ import (

"github.com/traPtitech/knoQ/domain"
"github.com/traPtitech/knoQ/infra/db"
"github.com/traPtitech/knoQ/repository"
"github.com/traPtitech/knoQ/infra/traq"
"github.com/traPtitech/knoQ/repository"
"github.com/traPtitech/knoQ/utils"
"golang.org/x/oauth2"

Expand All @@ -32,7 +32,8 @@ var (
mariadbPassword = getenv("MARIADB_PASSWORD", "password")
mariadbDatabase = getenv("MARIADB_DATABASE", "knoQ")
mariadbPort = getenv("MARIADB_PORT", "3306")
tokenKey = getenv("TOKEN_KEY", "random32wordsXXXXXXXXXXXXXXXXXXX")
tokenKey = mustGetenv("TOKEN_KEY")
jwtTokenKey = mustGetenv("JWT_TOKEN_KEY")
gormLogLevel = getenv("GORM_LOG_LEVEL", "silent")

clientID = getenv("CLIENT_ID", "client_id")
Expand Down Expand Up @@ -89,6 +90,7 @@ func main() {
ActivityChannelID: activityChannelID,
DailyChannelId: dailyChannelID,
Origin: origin,
JWTTokenKey: jwtTokenKey,
}

e := handler.SetupRoute()
Expand Down Expand Up @@ -120,3 +122,10 @@ func getenv(key, fallback string) string {
}
return fallback
}

func mustGetenv(key string) string {
if value, ok := os.LookupEnv(key); ok {
return value
}
panic("environment variable " + key + " is not set")
}
4 changes: 1 addition & 3 deletions router/authorization.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@ import (
"github.com/traPtitech/knoQ/utils/random"
)

const JWTSecret = "jwtsecret"

var verifierCache = cache.New(5*time.Minute, 10*time.Minute)
var stateCache = cache.New(5*time.Minute, 10*time.Minute)

Expand Down Expand Up @@ -99,7 +97,7 @@ func (h *Handlers) HandleCreateToken(c echo.Context) error {

token := jwt.NewWithClaims(jwt.SigningMethodHS256, claims)

signedToken, err := token.SignedString([]byte(JWTSecret))
signedToken, err := token.SignedString([]byte(h.JWTTokenKey))
if err != nil {
return internalServerError(err)
}
Expand Down
2 changes: 1 addition & 1 deletion router/middleware.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ func ServerVersionMiddleware(version string) echo.MiddlewareFunc {
func (h *Handlers) JWTMiddleware() echo.MiddlewareFunc {
return echojwt.WithConfig(
echojwt.Config{
SigningKey: []byte(JWTSecret),
SigningKey: []byte(h.JWTTokenKey),
SuccessHandler: func(c echo.Context) {
// jwtの検証に成功したらsessionにuserIDを保存
sess, _ := session.Get("session", c)
Expand Down
1 change: 1 addition & 0 deletions router/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ type Handlers struct {
ActivityChannelID string
DailyChannelId string
Origin string
JWTTokenKey string
}

func (h *Handlers) SetupRoute() *echo.Echo {
Expand Down

0 comments on commit cbbdbed

Please sign in to comment.