Skip to content

Commit

Permalink
update for renew OTID
Browse files Browse the repository at this point in the history
  • Loading branch information
zensh committed Oct 14, 2020
1 parent 68e44de commit 1b5ba0f
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 1 deletion.
1 change: 1 addition & 0 deletions config/default.yml
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ auth_keys:
hid_key: q7FltzZWfvGIrdEdHYY # 一旦设定,尽量不要改变,否则派生出去的 HID 无法识别
open_trust:
otid: ""
legacy_otid: ""
private_keys: []
domain_public_keys:
- '{"kty":"RSA","alg":"PS256","e":"AQAB","kid":"4PblNZYSnOsy8sD6SHZPEl6DCqEerpgfi_sPxthHpWM","n":"0FjUWU9H6P9JTe3ZFOGxoVlYKFlzr98N44vIvjvvLVM1FU3MECJeTpztgnONZKelBO2YSY29v1mTl_PLWxVsn-gwkRczp1F5ogvt64dkPpaSdzpOLS1aKhqJSpVJp-D0lJWJ4ksEvyvM1hMNe9F3gbI6yyLigPhfF6qPdS2PxbFdilX4TmvrmViFnkVT31L4aXVuaEg9juLfxbIs-lnbvE9_L0a-zm-PfN-sLP3_SrPtUBLRH-cVgiMc43eXqU1H5AqJ0XzPHdrwzTRFiZuLsyaI2zj67D2x9Wwn8ze2OeP_B6th97XQfS_6zJ5BDs_VPoQi19F0Ts3dWnlXi2CrhQ"}'
1 change: 1 addition & 0 deletions src/conf/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ type SQL struct {
// OpenTrust ...
type OpenTrust struct {
OTID otgo.OTID `json:"otid" yaml:"otid"`
LegacyOTID otgo.OTID `json:"legacy_otid" yaml:"legacy_otid"`
PrivateKeys []string `json:"private_keys" yaml:"private_keys"`
DomainPublicKeys []string `json:"domain_public_keys" yaml:"domain_public_keys"`
}
Expand Down
13 changes: 12 additions & 1 deletion src/middleware/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,21 @@ func init() {
logging.Panicf("Parse Open Trust config failed: %s", err)
}
}
if err := otConf.LegacyOTID.Validate(); err == nil {
otLegacyVerifier, err = otgo.NewVerifier(conf.Config.GlobalCtx, otConf.LegacyOTID, false,
otConf.DomainPublicKeys...)
if err != nil {
logging.Panicf("Parse Open Trust config failed: %s", err)
}
}

if otVerifier == nil && Auther == nil {
logging.Warningf("`auth_keys` is empty, Auth middleware will not be executed.")
}
}

var otVerifier *otgo.Verifier
var otLegacyVerifier *otgo.Verifier

// Auther 是基于 JWT 的身份验证,当 config.auth_keys 配置了才会启用
var Auther *auth.Auth
Expand All @@ -47,14 +55,17 @@ func Auth(ctx *gear.Context) error {
}

vid, err := otVerifier.ParseOTVID(token)
if err != nil && otLegacyVerifier != nil {
vid, err = otLegacyVerifier.ParseOTVID(token)
}
if err != nil {
if Auther != nil { // 兼容老的 jwt 验证
return oldAuth(ctx)
}
return gear.ErrUnauthorized.WithMsg("authorization token verification failed")
}

logging.AccessLogger.SetTo(ctx, "otSub", vid.ID.String())
logging.AccessLogger.SetTo(ctx, "subject", vid.ID.String())
return nil
}
return oldAuth(ctx)
Expand Down

0 comments on commit 1b5ba0f

Please sign in to comment.