Skip to content

Commit 716f308

Browse files
authored
Config validation (#96)
1 parent c5e2055 commit 716f308

6 files changed

Lines changed: 83 additions & 19 deletions

File tree

cmd/log.go

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import (
1313

1414
"github.com/go-jose/go-jose/v3"
1515
"github.com/go-jose/go-jose/v3/jwt"
16+
"github.com/go-playground/validator/v10"
1617

1718
"github.com/cloudentity/oauth2c/internal/oauth2"
1819
"github.com/grantae/certinfo"
@@ -77,7 +78,16 @@ func LogBox(title string, msg string, args ...interface{}) {
7778
}
7879

7980
func LogError(err error) {
80-
pterm.Error.PrintOnError(err)
81+
switch e := err.(type) {
82+
case validator.ValidationErrors:
83+
trans := e.Translate(Trans)
84+
85+
for _, v := range trans {
86+
pterm.Error.Println(v)
87+
}
88+
default:
89+
pterm.Error.PrintOnError(err)
90+
}
8191
}
8292

8393
func LogWarning(msg string) {

cmd/oauth2.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,11 @@ func (c *OAuth2Cmd) Run(cconfig *oauth2.ClientConfig) func(cmd *cobra.Command, a
109109
cconfig.IssuerURL = strings.TrimSuffix(args[0], oauth2.OpenIDConfigurationPath)
110110
}
111111

112+
if err := Validate.Struct(cconfig); err != nil {
113+
LogError(err)
114+
os.Exit(1)
115+
}
116+
112117
if silent {
113118
browser.Stdout = io.Discard
114119
} else {

cmd/validator.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package cmd
2+
3+
import (
4+
"github.com/go-playground/locales/en"
5+
ut "github.com/go-playground/universal-translator"
6+
"github.com/go-playground/validator/v10"
7+
en_translations "github.com/go-playground/validator/v10/translations/en"
8+
)
9+
10+
var (
11+
Validate *validator.Validate
12+
Trans ut.Translator
13+
)
14+
15+
func init() {
16+
en := en.New()
17+
uni := ut.New(en, en)
18+
Trans, _ = uni.GetTranslator("en")
19+
20+
Validate = validator.New()
21+
22+
err := en_translations.RegisterDefaultTranslations(Validate, Trans)
23+
24+
if err != nil {
25+
panic(err)
26+
}
27+
}

go.mod

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@ go 1.21
55
require (
66
github.com/cli/browser v1.3.1-0.20231006085632-b393df5d7bfa
77
github.com/go-jose/go-jose/v3 v3.0.1
8+
github.com/go-playground/locales v0.14.1
9+
github.com/go-playground/universal-translator v0.18.1
10+
github.com/go-playground/validator/v10 v10.17.0
811
github.com/golang-jwt/jwt/v4 v4.4.3
912
github.com/google/uuid v1.3.0
1013
github.com/grantae/certinfo v0.0.0-20170412194111-59d56a35515b
@@ -15,7 +18,7 @@ require (
1518
github.com/pkg/errors v0.9.1
1619
github.com/pterm/pterm v0.12.42
1720
github.com/spf13/cobra v1.5.0
18-
github.com/stretchr/testify v1.8.0
21+
github.com/stretchr/testify v1.8.2
1922
github.com/tidwall/pretty v1.2.0
2023
)
2124

@@ -24,17 +27,20 @@ require (
2427
atomicgo.dev/keyboard v0.2.8 // indirect
2528
github.com/containerd/console v1.0.3 // indirect
2629
github.com/davecgh/go-spew v1.1.1 // indirect
30+
github.com/gabriel-vasile/mimetype v1.4.2 // indirect
2731
github.com/gookit/color v1.5.0 // indirect
2832
github.com/hashicorp/errwrap v1.0.0 // indirect
2933
github.com/inconshreveable/mousetrap v1.0.0 // indirect
3034
github.com/itchyny/timefmt-go v0.1.5 // indirect
35+
github.com/leodido/go-urn v1.2.4 // indirect
3136
github.com/lithammer/fuzzysearch v1.1.5 // indirect
3237
github.com/mattn/go-runewidth v0.0.14 // indirect
3338
github.com/pmezard/go-difflib v1.0.0 // indirect
3439
github.com/rivo/uniseg v0.2.0 // indirect
3540
github.com/spf13/pflag v1.0.5 // indirect
3641
github.com/xo/terminfo v0.0.0-20210125001918-ca9a967f8778 // indirect
3742
golang.org/x/crypto v0.17.0 // indirect
43+
golang.org/x/net v0.10.0 // indirect
3844
golang.org/x/sys v0.15.0 // indirect
3945
golang.org/x/term v0.15.0 // indirect
4046
golang.org/x/text v0.14.0 // indirect

go.sum

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,18 @@ github.com/cpuguy83/go-md2man/v2 v2.0.2/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46t
1919
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
2020
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
2121
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
22+
github.com/gabriel-vasile/mimetype v1.4.2 h1:w5qFW6JKBz9Y393Y4q372O9A7cUSequkh1Q7OhCmWKU=
23+
github.com/gabriel-vasile/mimetype v1.4.2/go.mod h1:zApsH/mKG4w07erKIaJPFiX0Tsq9BFQgN3qGY5GnNgA=
2224
github.com/go-jose/go-jose/v3 v3.0.1 h1:pWmKFVtt+Jl0vBZTIpz/eAKwsm6LkIxDVVbFHKkchhA=
2325
github.com/go-jose/go-jose/v3 v3.0.1/go.mod h1:RNkWWRld676jZEYoV3+XK8L2ZnNSvIsxFMht0mSX+u8=
26+
github.com/go-playground/assert/v2 v2.2.0 h1:JvknZsQTYeFEAhQwI4qEt9cyV5ONwRHC+lYKSsYSR8s=
27+
github.com/go-playground/assert/v2 v2.2.0/go.mod h1:VDjEfimB/XKnb+ZQfWdccd7VUvScMdVu0Titje2rxJ4=
28+
github.com/go-playground/locales v0.14.1 h1:EWaQ/wswjilfKLTECiXz7Rh+3BjFhfDFKv/oXslEjJA=
29+
github.com/go-playground/locales v0.14.1/go.mod h1:hxrqLVvrK65+Rwrd5Fc6F2O76J/NuW9t0sjnWqG1slY=
30+
github.com/go-playground/universal-translator v0.18.1 h1:Bcnm0ZwsGyWbCzImXv+pAJnYK9S473LQFuzCbDbfSFY=
31+
github.com/go-playground/universal-translator v0.18.1/go.mod h1:xekY+UJKNuX9WP91TpwSH2VMlDf28Uj24BCp08ZFTUY=
32+
github.com/go-playground/validator/v10 v10.17.0 h1:SmVVlfAOtlZncTxRuinDPomC2DkXJ4E5T9gDA0AIH74=
33+
github.com/go-playground/validator/v10 v10.17.0/go.mod h1:9iXMNT7sEkjXb0I+enO7QXmzG6QCsPWY4zveKFVRSyU=
2434
github.com/golang-jwt/jwt/v4 v4.4.3 h1:Hxl6lhQFj4AnOX6MLrsCb/+7tCj7DxP7VA+2rDIq5AU=
2535
github.com/golang-jwt/jwt/v4 v4.4.3/go.mod h1:m21LjoU+eqJr34lmDMbreY2eSTRJ1cv77w39/MY0Ch0=
2636
github.com/google/go-cmp v0.5.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
@@ -54,6 +64,8 @@ github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORN
5464
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
5565
github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE=
5666
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
67+
github.com/leodido/go-urn v1.2.4 h1:XlAE/cm/ms7TE/VMVoduSpNBoyc2dOxHs5MZSwAN63Q=
68+
github.com/leodido/go-urn v1.2.4/go.mod h1:7ZrI8mTSeBSHl/UaRyKQW1qZeMgak41ANeCNaVckg+4=
5769
github.com/lithammer/fuzzysearch v1.1.5 h1:Ag7aKU08wp0R9QCfF4GoGST9HbmAIeLP7xwMrOBEp1c=
5870
github.com/lithammer/fuzzysearch v1.1.5/go.mod h1:1R1LRNk7yKid1BaQkmuLQaHruxcC4HmAH30Dh61Ih1Q=
5971
github.com/lithammer/shortuuid/v4 v4.0.0 h1:QRbbVkfgNippHOS8PXDkti4NaWeyYfcBTHtw7k08o4c=
@@ -85,12 +97,14 @@ github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA=
8597
github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
8698
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
8799
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
100+
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
88101
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
89102
github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
90103
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
91104
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
92-
github.com/stretchr/testify v1.8.0 h1:pSgiaMZlXftHpm5L7V1+rVB+AZJydKsMxsQBIJw4PKk=
93105
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
106+
github.com/stretchr/testify v1.8.2 h1:+h33VjcLVPDHtOdpUCuF+7gSuG3yGIftsP1YvFihtJ8=
107+
github.com/stretchr/testify v1.8.2/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
94108
github.com/tidwall/pretty v1.2.0 h1:RWIZEg2iJ8/g6fDDYzMpobmaoGh5OLl4AXtGUGPcqCs=
95109
github.com/tidwall/pretty v1.2.0/go.mod h1:ITEVvHYasfjBbM0u2Pg8T2nJnzm8xPwvNhhsoaGGjNU=
96110
github.com/xo/terminfo v0.0.0-20210125001918-ca9a967f8778 h1:QldyIu/L63oPpyvQmHgvgickp1Yw510KJOqX7H24mg8=
@@ -100,6 +114,8 @@ golang.org/x/crypto v0.0.0-20190911031432-227b76d455e7/go.mod h1:yigFU9vqHzYiE8U
100114
golang.org/x/crypto v0.17.0 h1:r8bRNjWL3GshPW3gkd+RpvzWrZAwPS49OmTGZ/uhM4k=
101115
golang.org/x/crypto v0.17.0/go.mod h1:gCAAfMLgwOJRpTjQ2zCCt2OcSfYMTeZVSRtQlPC7Nq4=
102116
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
117+
golang.org/x/net v0.10.0 h1:X2//UzNDwYmtCLn7To6G58Wr6f5ahEAQgKNzv9Y951M=
118+
golang.org/x/net v0.10.0/go.mod h1:0qNGK6F8kojg2nk9dLZ2mShWaEBan6FAoqfSigmmuDg=
103119
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
104120
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
105121
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=

internal/oauth2/oauth2.go

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -51,43 +51,43 @@ const CodeVerifierLength = 43
5151
var CodeChallengeEncoder = base64.RawURLEncoding
5252

5353
type ClientConfig struct {
54-
IssuerURL string
55-
RedirectURL string
56-
GrantType string
54+
IssuerURL string `validate:"url"`
55+
RedirectURL string `validate:"url"`
56+
GrantType string `validate:"oneof=authorization_code client_credentials implicit password refresh_token urn:ietf:params:oauth:grant-type:jwt-bearer urn:ietf:params:oauth:grant-type:token-exchange urn:ietf:params:oauth:grant-type:device_code"`
5757
ClientID string
5858
ClientSecret string
5959
Scopes []string
6060
ACRValues []string
6161
Audience []string
62-
AuthMethod string
62+
AuthMethod string `validate:"omitempty,oneof=client_secret_basic client_secret_post client_secret_jwt private_key_jwt self_signed_tls_client_auth tls_client_auth none"`
6363
PKCE bool
6464
PAR bool
6565
RequestObject bool
6666
EncryptedRequestObject bool
6767
Insecure bool
68-
ResponseType []string
69-
ResponseMode string
68+
ResponseType []string `validate:"dive,omitempty,oneof=code id_token token"`
69+
ResponseMode string `validate:"omitempty,oneof=query form_post query.jwt form_post.jwt jwt"`
7070
Username string
7171
Password string
7272
RefreshToken string
73-
Assertion string
74-
SigningKey string
75-
EncryptionKey string
73+
Assertion string `validate:"omitempty,json"`
74+
SigningKey string `validate:"omitempty,uri"`
75+
EncryptionKey string `validate:"omitempty,uri"`
7676
SubjectToken string
77-
SubjectTokenType string
77+
SubjectTokenType string `validate:"omitempty,oneof=urn:ietf:params:oauth:token-type:access_token"`
7878
ActorToken string
79-
ActorTokenType string
79+
ActorTokenType string `validate:"omitempty,oneof=urn:ietf:params:oauth:token-type:access_token"`
8080
IDTokenHint string
8181
LoginHint string
8282
IDPHint string
83-
TLSCert string
84-
TLSKey string
85-
TLSRootCA string
83+
TLSCert string `validate:"omitempty,uri"`
84+
TLSKey string `validate:"omitempty,uri"`
85+
TLSRootCA string `validate:"omitempty,uri"`
8686
HTTPTimeout time.Duration
8787
BrowserTimeout time.Duration
8888
DPoP bool
89-
Claims string
90-
RAR string
89+
Claims string `validate:"omitempty,json"`
90+
RAR string `validate:"omitempty,json"`
9191
Purpose string
9292
}
9393

0 commit comments

Comments
 (0)