Skip to content

Commit ceb62fb

Browse files
committed
Move jwt keys to database
Moved code for it to one place Updated to chi v5
1 parent 2999011 commit ceb62fb

File tree

12 files changed

+168
-139
lines changed

12 files changed

+168
-139
lines changed

backend/cmd/server/main.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import (
1414
"npm/internal/entity/user"
1515
"npm/internal/errors"
1616
"npm/internal/jobqueue"
17+
"npm/internal/jwt"
1718
"npm/internal/logger"
1819
)
1920

@@ -26,6 +27,11 @@ func main() {
2627
config.Init(&version, &commit, &sentryDSN)
2728

2829
database.Migrate(func() {
30+
if err := jwt.LoadKeys(); err != nil {
31+
logger.Error("KeysError", err)
32+
os.Exit(1)
33+
}
34+
2935
setting.ApplySettings()
3036
checkSetup()
3137

backend/embed/migrations/sqlite/20201013035318_initial_schema.sql

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,15 @@
11
-- migrate:up
22

3+
CREATE TABLE IF NOT EXISTS `keys`
4+
(
5+
id INTEGER PRIMARY KEY AUTOINCREMENT,
6+
created_at INTEGER NOT NULL DEFAULT 0,
7+
updated_at INTEGER NOT NULL DEFAULT 0,
8+
is_deleted INTEGER NOT NULL DEFAULT 0,
9+
public_key TEXT NOT NULL,
10+
private_key TEXT NOT NULL
11+
);
12+
313
CREATE TABLE IF NOT EXISTS `user`
414
(
515
id INTEGER PRIMARY KEY AUTOINCREMENT,

backend/go.mod

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,10 @@ require (
1212
github.com/getsentry/sentry-go v0.21.0
1313
github.com/glebarez/sqlite v1.8.0
1414
github.com/go-chi/chi v4.1.2+incompatible
15+
github.com/go-chi/chi/v5 v5.0.8
1516
github.com/go-chi/cors v1.2.1
1617
github.com/go-chi/jwtauth v4.0.4+incompatible
18+
github.com/go-chi/jwtauth/v5 v5.1.0
1719
github.com/jc21/go-sse v0.0.0-20230307071053-2e6b1dbcb7ec
1820
github.com/jc21/jsref v0.0.0-20210608024405-a97debfc4760
1921
github.com/patrickmn/go-cache v2.1.0+incompatible
@@ -32,16 +34,23 @@ require (
3234
require (
3335
github.com/alexflint/go-scalar v1.2.0 // indirect
3436
github.com/davecgh/go-spew v1.1.1 // indirect
37+
github.com/decred/dcrd/dcrec/secp256k1/v4 v4.1.0 // indirect
3538
github.com/dustin/go-humanize v1.0.1 // indirect
3639
github.com/glebarez/go-sqlite v1.21.1 // indirect
3740
github.com/go-sql-driver/mysql v1.7.1 // indirect
41+
github.com/goccy/go-json v0.9.11 // indirect
3842
github.com/google/uuid v1.3.0 // indirect
3943
github.com/jackc/pgpassfile v1.0.0 // indirect
4044
github.com/jackc/pgservicefile v0.0.0-20221227161230-091c0ba34f0a // indirect
4145
github.com/jackc/pgx/v5 v5.3.1 // indirect
4246
github.com/jinzhu/inflection v1.0.0 // indirect
4347
github.com/jinzhu/now v1.1.5 // indirect
48+
github.com/lestrrat-go/blackmagic v1.0.1 // indirect
49+
github.com/lestrrat-go/httpcc v1.0.1 // indirect
50+
github.com/lestrrat-go/httprc v1.0.4 // indirect
51+
github.com/lestrrat-go/iter v1.0.2 // indirect
4452
github.com/lestrrat-go/jspointer v0.0.0-20181205001929-82fadba7561c // indirect
53+
github.com/lestrrat-go/jwx/v2 v2.0.6 // indirect
4554
github.com/lestrrat-go/option v1.0.1 // indirect
4655
github.com/lestrrat-go/pdebug/v3 v3.0.1 // indirect
4756
github.com/lestrrat-go/structinfo v0.0.0-20210312050401-7f8bd69d6acb // indirect

backend/go.sum

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@ github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ3
1111
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
1212
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
1313
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
14+
github.com/decred/dcrd/crypto/blake256 v1.0.0/go.mod h1:sQl2p6Y26YV+ZOcSTP6thNdn47hh8kt6rqSlvmrXFAc=
15+
github.com/decred/dcrd/dcrec/secp256k1/v4 v4.1.0 h1:HbphB4TFFXpv7MNrT52FGrrgVXF1owhMVTHFZIlnvd4=
16+
github.com/decred/dcrd/dcrec/secp256k1/v4 v4.1.0/go.mod h1:DZGJHZMqrU4JJqFAWUS2UO1+lbSKsdiOoYi9Zzey7Fc=
1417
github.com/dgrijalva/jwt-go v3.2.0+incompatible h1:7qlOGliEKZXTDg6OTjfoBKDXWrumCAMpl/TFQ4/5kLM=
1518
github.com/dgrijalva/jwt-go v3.2.0+incompatible/go.mod h1:E3ru+11k8xSBh+hMPgOLZmtrrCbhqsmaPHjLKYnJCaQ=
1619
github.com/drexedam/gravatar v0.0.0-20210327211422-e94eea8c338e h1:2R8DvYLNr5DL25eWwpOdPno1eIbTNjJC0d7v8ti5cus=
@@ -27,14 +30,20 @@ github.com/glebarez/sqlite v1.8.0 h1:02X12E2I/4C1n+v90yTqrjRa8yuo7c3KeHI3FRznCvc
2730
github.com/glebarez/sqlite v1.8.0/go.mod h1:bpET16h1za2KOOMb8+jCp6UBP/iahDpfPQqSaYLTLx8=
2831
github.com/go-chi/chi v4.1.2+incompatible h1:fGFk2Gmi/YKXk0OmGfBh0WgmN3XB8lVnEyNz34tQRec=
2932
github.com/go-chi/chi v4.1.2+incompatible/go.mod h1:eB3wogJHnLi3x/kFX2A+IbTBlXxmMeXJVKy9tTv1XzQ=
33+
github.com/go-chi/chi/v5 v5.0.8 h1:lD+NLqFcAi1ovnVZpsnObHGW4xb4J8lNmoYVfECH1Y0=
34+
github.com/go-chi/chi/v5 v5.0.8/go.mod h1:DslCQbL2OYiznFReuXYUmQ2hGd1aDpCnlMNITLSKoi8=
3035
github.com/go-chi/cors v1.2.1 h1:xEC8UT3Rlp2QuWNEr4Fs/c2EAGVKBwy/1vHx3bppil4=
3136
github.com/go-chi/cors v1.2.1/go.mod h1:sSbTewc+6wYHBBCW7ytsFSn836hqM7JxpglAy2Vzc58=
3237
github.com/go-chi/jwtauth v4.0.4+incompatible h1:LGIxg6YfvSBzxU2BljXbrzVc1fMlgqSKBQgKOGAVtPY=
3338
github.com/go-chi/jwtauth v4.0.4+incompatible/go.mod h1:Q5EIArY/QnD6BdS+IyDw7B2m6iNbnPxtfd6/BcmtWbs=
39+
github.com/go-chi/jwtauth/v5 v5.1.0 h1:wJyf2YZ/ohPvNJBwPOzZaQbyzwgMZZceE1m8FOzXLeA=
40+
github.com/go-chi/jwtauth/v5 v5.1.0/go.mod h1:MA93hc1au3tAQwCKry+fI4LqJ5MIVN4XSsglOo+lSc8=
3441
github.com/go-errors/errors v1.4.2 h1:J6MZopCL4uSllY1OfXM374weqZFFItUbrImctkmUxIA=
3542
github.com/go-sql-driver/mysql v1.7.0/go.mod h1:OXbVy3sEdcQ2Doequ6Z5BW6fXNQTmx+9S1MCJN5yJMI=
3643
github.com/go-sql-driver/mysql v1.7.1 h1:lUIinVbN1DY0xBg0eMOzmmtGoHwWBbvnWubQUrtU8EI=
3744
github.com/go-sql-driver/mysql v1.7.1/go.mod h1:OXbVy3sEdcQ2Doequ6Z5BW6fXNQTmx+9S1MCJN5yJMI=
45+
github.com/goccy/go-json v0.9.11 h1:/pAaQDLHEoCq/5FFmSKBswWmK6H0e8g4159Kc/X/nqk=
46+
github.com/goccy/go-json v0.9.11/go.mod h1:6MelG93GURQebXPDq3khkgXZkazVtN9CRI+MGFi0w8I=
3847
github.com/golang-sql/civil v0.0.0-20220223132316-b832511892a9 h1:au07oEsX2xN0ktxqI+Sida1w446QrXBRJ0nee3SNZlA=
3948
github.com/golang-sql/sqlexp v0.1.0 h1:ZCD6MBpcuOVfGVqsEmY5/4FtYiKz6tSyUv9LPEDei6A=
4049
github.com/google/go-cmp v0.5.9 h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38=
@@ -64,9 +73,20 @@ github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
6473
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
6574
github.com/kyoh86/richgo v0.3.8/go.mod h1:2C8POkF1H04iTOG2Tp1yyZhspCME9nN3cir3VXJ02II=
6675
github.com/kyoh86/xdg v1.2.0/go.mod h1:/mg8zwu1+qe76oTFUBnyS7rJzk7LLC0VGEzJyJ19DHs=
76+
github.com/lestrrat-go/blackmagic v1.0.1 h1:lS5Zts+5HIC/8og6cGHb0uCcNCa3OUt1ygh3Qz2Fe80=
77+
github.com/lestrrat-go/blackmagic v1.0.1/go.mod h1:UrEqBzIR2U6CnzVyUtfM6oZNMt/7O7Vohk2J0OGSAtU=
78+
github.com/lestrrat-go/httpcc v1.0.1 h1:ydWCStUeJLkpYyjLDHihupbn2tYmZ7m22BGkcvZZrIE=
79+
github.com/lestrrat-go/httpcc v1.0.1/go.mod h1:qiltp3Mt56+55GPVCbTdM9MlqhvzyuL6W/NMDA8vA5E=
80+
github.com/lestrrat-go/httprc v1.0.4 h1:bAZymwoZQb+Oq8MEbyipag7iSq6YIga8Wj6GOiJGdI8=
81+
github.com/lestrrat-go/httprc v1.0.4/go.mod h1:mwwz3JMTPBjHUkkDv/IGJ39aALInZLrhBp0X7KGUZlo=
82+
github.com/lestrrat-go/iter v1.0.2 h1:gMXo1q4c2pHmC3dn8LzRhJfP1ceCbgSiT9lUydIzltI=
83+
github.com/lestrrat-go/iter v1.0.2/go.mod h1:Momfcq3AnRlRjI5b5O8/G5/BvpzrhoFTZcn06fEOPt4=
6784
github.com/lestrrat-go/jspointer v0.0.0-20181205001929-82fadba7561c h1:pGh5EFIfczeDHwgMHgfwjhZzL+8/E3uZF6T7vER/W8c=
6885
github.com/lestrrat-go/jspointer v0.0.0-20181205001929-82fadba7561c/go.mod h1:xw2Gm4Mg+ST9s8fHR1VkUIyOJMJnSloRZlPQB+wyVpY=
86+
github.com/lestrrat-go/jwx/v2 v2.0.6 h1:RlyYNLV892Ed7+FTfj1ROoF6x7WxL965PGTHso/60G0=
87+
github.com/lestrrat-go/jwx/v2 v2.0.6/go.mod h1:aVrGuwEr3cp2Prw6TtQvr8sQxe+84gruID5C9TxT64Q=
6988
github.com/lestrrat-go/option v0.0.0-20210103042652-6f1ecfceda35/go.mod h1:5ZHFbivi4xwXxhxY9XHDe2FHo6/Z7WWmtT7T5nBBp3I=
89+
github.com/lestrrat-go/option v1.0.0/go.mod h1:5ZHFbivi4xwXxhxY9XHDe2FHo6/Z7WWmtT7T5nBBp3I=
7090
github.com/lestrrat-go/option v1.0.1 h1:oAzP2fvZGQKWkvHa1/SAcFolBEca1oN+mQ7eooNBEYU=
7191
github.com/lestrrat-go/option v1.0.1/go.mod h1:5ZHFbivi4xwXxhxY9XHDe2FHo6/Z7WWmtT7T5nBBp3I=
7292
github.com/lestrrat-go/pdebug/v3 v3.0.1 h1:3G5sX/aw/TbMTtVc9U7IHBWRZtMvwvBziF1e4HoQtv8=
@@ -126,18 +146,25 @@ github.com/vrischmann/envconfig v1.3.0 h1:4XIvQTXznxmWMnjouj0ST5lFo/WAYf5Exgl3x8
126146
github.com/vrischmann/envconfig v1.3.0/go.mod h1:bbvxFYJdRSpXrhS63mBFtKJzkDiNkyArOLXtY6q0kuI=
127147
github.com/wacul/ptr v1.0.0/go.mod h1:BD0gjsZrCwtoR+yWDB9v2hQ8STlq9tT84qKfa+3txOc=
128148
github.com/zenizh/go-capturer v0.0.0-20211219060012-52ea6c8fed04 h1:qXafrlZL1WsJW5OokjraLLRURHiw0OzKHD/RNdspp4w=
149+
golang.org/x/crypto v0.0.0-20220427172511-eb4f295cb31f/go.mod h1:IxCIyHEi3zRg3s0A5j5BB6A9Jmi73HwBIUl50j+osU4=
129150
golang.org/x/crypto v0.9.0 h1:LF6fAI+IutBocDJ2OT0Q1g8plpYljMZ4+lty+dsqw3g=
130151
golang.org/x/crypto v0.9.0/go.mod h1:yrmDGqONDYtNj3tH8X9dzUun2m2lzPa9ngI6/RUPGR0=
131-
golang.org/x/net v0.10.0 h1:X2//UzNDwYmtCLn7To6G58Wr6f5ahEAQgKNzv9Y951M=
152+
golang.org/x/net v0.0.0-20211112202133-69e39bad7dc2/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
132153
golang.org/x/sys v0.0.0-20200116001909-b77594299b42/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
154+
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
133155
golang.org/x/sys v0.0.0-20210326220804-49726bf1d181/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
156+
golang.org/x/sys v0.0.0-20210423082822-04245dca01da/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
134157
golang.org/x/sys v0.0.0-20210603125802-9665404d3644/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
158+
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
135159
golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
136160
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
137161
golang.org/x/sys v0.8.0 h1:EBmGv8NaZBZTWvrbjNoL6HVt+IVy3QDQpJs7VRIw3tU=
138162
golang.org/x/sys v0.8.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
163+
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
164+
golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
139165
golang.org/x/text v0.9.0 h1:2sjJmO8cDvYveuX97RDLsxlyUxLl+GHoLxBiRdHllBE=
140166
golang.org/x/text v0.9.0/go.mod h1:e1OnstbJyHTd6l/uOt8jFFHp6TRDWZR/bV3emEE/zU8=
167+
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
141168
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
142169
gopkg.in/check.v1 v1.0.0-20200227125254-8fa46927fb4f/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
143170
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk=

backend/internal/api/middleware/auth.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import (
1313
"npm/internal/logger"
1414
"npm/internal/util"
1515

16-
"github.com/go-chi/jwtauth"
16+
"github.com/go-chi/jwtauth/v5"
1717
)
1818

1919
// DecodeAuth decodes an auth header
@@ -29,7 +29,7 @@ func DecodeAuth() func(http.Handler) http.Handler {
2929
}
3030

3131
tokenAuth := jwtauth.New("RS256", privateKey, publicKey)
32-
return jwtauth.Verifier(tokenAuth)
32+
return jwtauth.Verify(tokenAuth, jwtauth.TokenFromHeader)
3333
}
3434

3535
// Enforce is a authentication middleware to enforce access from the
@@ -44,13 +44,14 @@ func Enforce(permission string) func(http.Handler) http.Handler {
4444
token, claims, err := jwtauth.FromContext(ctx)
4545

4646
if err != nil {
47+
logger.Debug("EnforceError: %+v", err)
4748
h.ResultErrorJSON(w, r, http.StatusUnauthorized, err.Error(), nil)
4849
return
4950
}
5051

5152
userID := uint(claims["uid"].(float64))
5253
_, enabled := user.IsEnabled(userID)
53-
if token == nil || !token.Valid || !enabled {
54+
if token == nil || !enabled {
5455
h.ResultErrorJSON(w, r, http.StatusUnauthorized, "Unauthorised", nil)
5556
return
5657
}

backend/internal/api/middleware/cors.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import (
55
"net/http"
66
"strings"
77

8-
"github.com/go-chi/chi"
8+
"github.com/go-chi/chi/v5"
99
)
1010

1111
var methodMap = []string{

backend/internal/api/router.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ import (
1212
"npm/internal/logger"
1313
"npm/internal/serverevents"
1414

15-
"github.com/go-chi/chi"
16-
chiMiddleware "github.com/go-chi/chi/middleware"
15+
"github.com/go-chi/chi/v5"
16+
chiMiddleware "github.com/go-chi/chi/v5/middleware"
1717
"github.com/go-chi/cors"
1818
)
1919

backend/internal/config/config.go

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ func Init(version, commit, sentryDSN *string) {
2626
initLogger(*sentryDSN)
2727
logger.Info("Build Version: %s (%s)", Version, Commit)
2828
createDataFolders()
29-
loadKeys()
3029
}
3130

3231
// InitIPRanges will initialise the config for the ipranges command
@@ -79,11 +78,3 @@ func initLogger(sentryDSN string) {
7978
func GetLogLevel() logger.Level {
8079
return logLevel
8180
}
82-
83-
func isError(errorClass string, err error) bool {
84-
if err != nil {
85-
logger.Error(errorClass, err)
86-
return true
87-
}
88-
return false
89-
}

backend/internal/config/keys.go

Lines changed: 0 additions & 111 deletions
This file was deleted.

backend/internal/config/vars.go

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,6 @@ var IsSetup bool
1616
// ErrorReporting defines whether we will send errors to Sentry
1717
var ErrorReporting bool
1818

19-
// PublicKey is the public key
20-
var PublicKey string
21-
22-
// PrivateKey is the private key
23-
var PrivateKey string
24-
2519
var logLevel logger.Level
2620

2721
type log struct {

0 commit comments

Comments
 (0)