keys and more
This commit is contained in:
parent
92dee085b7
commit
3082234dae
9 changed files with 74 additions and 3 deletions
|
@ -1,6 +1,8 @@
|
||||||
package mock
|
package mock
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"crypto/rand"
|
||||||
|
"crypto/rsa"
|
||||||
"errors"
|
"errors"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
@ -11,6 +13,19 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
type Storage struct {
|
type Storage struct {
|
||||||
|
key *rsa.PrivateKey
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewStorage() op.Storage {
|
||||||
|
reader := rand.Reader
|
||||||
|
bitSize := 2048
|
||||||
|
key, err := rsa.GenerateKey(reader, bitSize)
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
return &Storage{
|
||||||
|
key: key,
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
type AuthRequest struct {
|
type AuthRequest struct {
|
||||||
|
@ -113,7 +128,15 @@ func (s *Storage) AuthRequestByID(id string) (op.AuthRequest, error) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *Storage) GetSigningKey() (*jose.SigningKey, error) {
|
func (s *Storage) GetSigningKey() (*jose.SigningKey, error) {
|
||||||
return &jose.SigningKey{Algorithm: jose.HS256, Key: []byte("test")}, nil
|
return &jose.SigningKey{Algorithm: jose.RS256, Key: s.key}, nil
|
||||||
|
}
|
||||||
|
func (s *Storage) GetKeySet() (jose.JSONWebKeySet, error) {
|
||||||
|
pubkey := s.key.Public()
|
||||||
|
return jose.JSONWebKeySet{
|
||||||
|
Keys: []jose.JSONWebKey{
|
||||||
|
jose.JSONWebKey{Key: pubkey, Use: "sig", Algorithm: "RS256"},
|
||||||
|
},
|
||||||
|
}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
type ConfClient struct {
|
type ConfClient struct {
|
||||||
|
|
|
@ -15,7 +15,7 @@ func main() {
|
||||||
|
|
||||||
Port: "9998",
|
Port: "9998",
|
||||||
}
|
}
|
||||||
storage := &mock.Storage{}
|
storage := mock.NewStorage()
|
||||||
handler, err := op.NewDefaultOP(config, storage, op.WithCustomTokenEndpoint("test"))
|
handler, err := op.NewDefaultOP(config, storage, op.WithCustomTokenEndpoint("test"))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
|
|
|
@ -11,6 +11,7 @@ type Configuration interface {
|
||||||
AuthorizationEndpoint() Endpoint
|
AuthorizationEndpoint() Endpoint
|
||||||
TokenEndpoint() Endpoint
|
TokenEndpoint() Endpoint
|
||||||
UserinfoEndpoint() Endpoint
|
UserinfoEndpoint() Endpoint
|
||||||
|
KeysEndpoint() Endpoint
|
||||||
Port() string
|
Port() string
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -14,6 +14,7 @@ const (
|
||||||
defaulTokenEndpoint = "oauth/token"
|
defaulTokenEndpoint = "oauth/token"
|
||||||
defaultIntrospectEndpoint = "introspect"
|
defaultIntrospectEndpoint = "introspect"
|
||||||
defaultUserinfoEndpoint = "userinfo"
|
defaultUserinfoEndpoint = "userinfo"
|
||||||
|
defaultKeysEndpoint = "keys"
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
|
@ -22,6 +23,7 @@ var (
|
||||||
Token: defaulTokenEndpoint,
|
Token: defaulTokenEndpoint,
|
||||||
IntrospectionEndpoint: defaultIntrospectEndpoint,
|
IntrospectionEndpoint: defaultIntrospectEndpoint,
|
||||||
Userinfo: defaultUserinfoEndpoint,
|
Userinfo: defaultUserinfoEndpoint,
|
||||||
|
JwksURI: defaultKeysEndpoint,
|
||||||
}
|
}
|
||||||
DefaultIDTokenValidity = time.Duration(5 * time.Minute)
|
DefaultIDTokenValidity = time.Duration(5 * time.Minute)
|
||||||
)
|
)
|
||||||
|
@ -146,6 +148,10 @@ func (p *DefaultOP) UserinfoEndpoint() Endpoint {
|
||||||
return Endpoint(p.endpoints.Userinfo)
|
return Endpoint(p.endpoints.Userinfo)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (p *DefaultOP) KeysEndpoint() Endpoint {
|
||||||
|
return Endpoint(p.endpoints.JwksURI)
|
||||||
|
}
|
||||||
|
|
||||||
func (p *DefaultOP) Port() string {
|
func (p *DefaultOP) Port() string {
|
||||||
return p.config.Port
|
return p.config.Port
|
||||||
}
|
}
|
||||||
|
@ -186,6 +192,10 @@ func (p *DefaultOP) IDTokenValidity() time.Duration {
|
||||||
// return AuthRequestError
|
// return AuthRequestError
|
||||||
// }
|
// }
|
||||||
|
|
||||||
|
func (p *DefaultOP) HandleKeys(w http.ResponseWriter, r *http.Request) {
|
||||||
|
Keys(w, r, p)
|
||||||
|
}
|
||||||
|
|
||||||
func (p *DefaultOP) HandleAuthorize(w http.ResponseWriter, r *http.Request) {
|
func (p *DefaultOP) HandleAuthorize(w http.ResponseWriter, r *http.Request) {
|
||||||
Authorize(w, r, p)
|
Authorize(w, r, p)
|
||||||
}
|
}
|
||||||
|
|
|
@ -20,7 +20,7 @@ func CreateDiscoveryConfig(c Configuration) *oidc.DiscoveryConfiguration {
|
||||||
UserinfoEndpoint: c.UserinfoEndpoint().Absolute(c.Issuer()),
|
UserinfoEndpoint: c.UserinfoEndpoint().Absolute(c.Issuer()),
|
||||||
// EndSessionEndpoint: c.TokenEndpoint().Absolute(c.Issuer())(c.EndSessionEndpoint),
|
// EndSessionEndpoint: c.TokenEndpoint().Absolute(c.Issuer())(c.EndSessionEndpoint),
|
||||||
// CheckSessionIframe: c.TokenEndpoint().Absolute(c.Issuer())(c.CheckSessionIframe),
|
// CheckSessionIframe: c.TokenEndpoint().Absolute(c.Issuer())(c.CheckSessionIframe),
|
||||||
// JwksURI: c.TokenEndpoint().Absolute(c.Issuer())(c.JwksURI),
|
JwksURI: c.KeysEndpoint().Absolute(c.Issuer()),
|
||||||
// ScopesSupported: oidc.SupportedScopes,
|
// ScopesSupported: oidc.SupportedScopes,
|
||||||
// ResponseTypesSupported: responseTypes,
|
// ResponseTypesSupported: responseTypes,
|
||||||
// GrantTypesSupported: oidc.SupportedGrantTypes,
|
// GrantTypesSupported: oidc.SupportedGrantTypes,
|
||||||
|
|
19
pkg/op/keys.go
Normal file
19
pkg/op/keys.go
Normal file
|
@ -0,0 +1,19 @@
|
||||||
|
package op
|
||||||
|
|
||||||
|
import (
|
||||||
|
"net/http"
|
||||||
|
|
||||||
|
"github.com/caos/oidc/pkg/utils"
|
||||||
|
)
|
||||||
|
|
||||||
|
type KeyProvider interface {
|
||||||
|
Storage() Storage
|
||||||
|
}
|
||||||
|
|
||||||
|
func Keys(w http.ResponseWriter, r *http.Request, k KeyProvider) {
|
||||||
|
keySet, err := k.Storage().GetKeySet()
|
||||||
|
if err != nil {
|
||||||
|
|
||||||
|
}
|
||||||
|
utils.MarshalJSON(w, keySet)
|
||||||
|
}
|
|
@ -139,6 +139,21 @@ func (mr *MockStorageMockRecorder) GetClientByClientID(arg0 interface{}) *gomock
|
||||||
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "GetClientByClientID", reflect.TypeOf((*MockStorage)(nil).GetClientByClientID), arg0)
|
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "GetClientByClientID", reflect.TypeOf((*MockStorage)(nil).GetClientByClientID), arg0)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// GetKeySet mocks base method
|
||||||
|
func (m *MockStorage) GetKeySet() (go_jose_v2.JSONWebKeySet, error) {
|
||||||
|
m.ctrl.T.Helper()
|
||||||
|
ret := m.ctrl.Call(m, "GetKeySet")
|
||||||
|
ret0, _ := ret[0].(go_jose_v2.JSONWebKeySet)
|
||||||
|
ret1, _ := ret[1].(error)
|
||||||
|
return ret0, ret1
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetKeySet indicates an expected call of GetKeySet
|
||||||
|
func (mr *MockStorageMockRecorder) GetKeySet() *gomock.Call {
|
||||||
|
mr.mock.ctrl.T.Helper()
|
||||||
|
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "GetKeySet", reflect.TypeOf((*MockStorage)(nil).GetKeySet))
|
||||||
|
}
|
||||||
|
|
||||||
// GetSigningKey mocks base method
|
// GetSigningKey mocks base method
|
||||||
func (m *MockStorage) GetSigningKey() (*go_jose_v2.SigningKey, error) {
|
func (m *MockStorage) GetSigningKey() (*go_jose_v2.SigningKey, error) {
|
||||||
m.ctrl.T.Helper()
|
m.ctrl.T.Helper()
|
||||||
|
|
|
@ -18,6 +18,7 @@ type OpenIDProvider interface {
|
||||||
HandleAuthorizeCallback(w http.ResponseWriter, r *http.Request)
|
HandleAuthorizeCallback(w http.ResponseWriter, r *http.Request)
|
||||||
HandleExchange(w http.ResponseWriter, r *http.Request)
|
HandleExchange(w http.ResponseWriter, r *http.Request)
|
||||||
HandleUserinfo(w http.ResponseWriter, r *http.Request)
|
HandleUserinfo(w http.ResponseWriter, r *http.Request)
|
||||||
|
HandleKeys(w http.ResponseWriter, r *http.Request)
|
||||||
// Storage() Storage
|
// Storage() Storage
|
||||||
HttpHandler() *http.Server
|
HttpHandler() *http.Server
|
||||||
}
|
}
|
||||||
|
@ -29,6 +30,7 @@ func CreateRouter(o OpenIDProvider) *mux.Router {
|
||||||
router.HandleFunc(o.AuthorizationEndpoint().Relative()+"/{id}", o.HandleAuthorizeCallback)
|
router.HandleFunc(o.AuthorizationEndpoint().Relative()+"/{id}", o.HandleAuthorizeCallback)
|
||||||
router.HandleFunc(o.TokenEndpoint().Relative(), o.HandleExchange)
|
router.HandleFunc(o.TokenEndpoint().Relative(), o.HandleExchange)
|
||||||
router.HandleFunc(o.UserinfoEndpoint().Relative(), o.HandleUserinfo)
|
router.HandleFunc(o.UserinfoEndpoint().Relative(), o.HandleUserinfo)
|
||||||
|
router.HandleFunc(o.KeysEndpoint().Relative(), o.HandleKeys)
|
||||||
return router
|
return router
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -17,6 +17,7 @@ type Storage interface {
|
||||||
AuthorizeClientIDCodeVerifier(string, string) (Client, error)
|
AuthorizeClientIDCodeVerifier(string, string) (Client, error)
|
||||||
DeleteAuthRequestAndCode(string, string) error
|
DeleteAuthRequestAndCode(string, string) error
|
||||||
GetSigningKey() (*jose.SigningKey, error)
|
GetSigningKey() (*jose.SigningKey, error)
|
||||||
|
GetKeySet() (jose.JSONWebKeySet, error)
|
||||||
}
|
}
|
||||||
|
|
||||||
type AuthRequest interface {
|
type AuthRequest interface {
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue