-
Notifications
You must be signed in to change notification settings - Fork 317
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Enhance Custom Authorization Abilities
- Loading branch information
1 parent
cc03474
commit c4e36b1
Showing
9 changed files
with
160 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package authz | ||
|
||
import ( | ||
"crypto/tls" | ||
"net" | ||
) | ||
|
||
// RequestAttributes represents attributes of a TURN request which | ||
// may be useful for authorizing the underlying request. | ||
type RequestAttributes struct { | ||
Username string | ||
Realm string | ||
SrcAddr net.Addr | ||
TLS *tls.ConnectionState | ||
|
||
// extend as needed | ||
} | ||
|
||
// Authorizer represents functionality required to authorize a request. | ||
type Authorizer interface { | ||
Authorize(ra *RequestAttributes) (key []byte, ok bool) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package authz | ||
|
||
import "net" | ||
|
||
// LegacyAuthFunc is a function used to authorize requests compatible with legacy authorization. | ||
type LegacyAuthFunc func(username, realm string, srcAddr net.Addr) (key []byte, ok bool) | ||
|
||
// legacyAuthorizer is the an Authorizer implementation | ||
// which wraps an AuthFunc in order to authorize requests. | ||
type legacyAuthorizer struct { | ||
authFunc LegacyAuthFunc | ||
} | ||
|
||
// NewLegacy returns a new legacy authorizer. | ||
func NewLegacy(fn LegacyAuthFunc) Authorizer { | ||
return &legacyAuthorizer{authFunc: fn} | ||
} | ||
|
||
// Authorize authorizes a request given request attributes. | ||
func (a *legacyAuthorizer) Authorize(ra *RequestAttributes) (key []byte, ok bool) { | ||
return a.authFunc(ra.Username, ra.Realm, ra.SrcAddr) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
package authz | ||
|
||
import ( | ||
"crypto/x509" | ||
) | ||
|
||
// tlsAuthorizer is the an Authorizer implementation which verifies | ||
// client TLS certificate metadata in order to to authorize requests. | ||
type tlsAuthorizer struct { | ||
verifyOpts x509.VerifyOptions | ||
getKeyForUserFunc func(string) ([]byte, bool) | ||
} | ||
|
||
// NewTLS returns a new client tls certificate authorizer. | ||
// | ||
// This authorizer ensures that the client presents a valid TLS certificate | ||
// for which the CommonName must match the TURN request's username attribute. | ||
func NewTLS( | ||
verifyOpts x509.VerifyOptions, | ||
getKeyForUserFunc func(string) ([]byte, bool), | ||
) Authorizer { | ||
return &tlsAuthorizer{ | ||
verifyOpts: verifyOpts, | ||
getKeyForUserFunc: getKeyForUserFunc, | ||
} | ||
} | ||
|
||
// Authorize authorizes a request given request attributes. | ||
func (a *tlsAuthorizer) Authorize(ra *RequestAttributes) ([]byte, bool) { | ||
if ra.TLS == nil || len(ra.TLS.PeerCertificates) == 0 { | ||
// request not allowed due to not having tls state metadata | ||
// TODO: INFO log | ||
return nil, false | ||
} | ||
|
||
key, ok := a.getKeyForUserFunc(ra.Username) | ||
if !ok { | ||
// request not allowed due to having no key for the TURN request's username | ||
// TODO: INFO log | ||
return nil, false | ||
} | ||
|
||
for _, cert := range ra.TLS.PeerCertificates { | ||
if cert.Subject.CommonName != ra.Username { | ||
// cert not allowed due to not matching the TURN username | ||
// TODO: DEBUG log | ||
continue | ||
} | ||
|
||
if _, err := cert.Verify(a.verifyOpts); err != nil { | ||
// cert not allowed due to failed validation | ||
// TODO: WARN log | ||
continue | ||
} | ||
|
||
// a valid certificate was allowed | ||
return key, true | ||
} | ||
|
||
// request not allowed due to not having any valid certs | ||
// TODO: INFO log | ||
return nil, false | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters