https://github.com/vzhn/netty-http-authenticator
Basic and digest HTTP authentication for Netty
https://github.com/vzhn/netty-http-authenticator
digest-authentication http netty rtsp
Last synced: 2 days ago
JSON representation
Basic and digest HTTP authentication for Netty
- Host: GitHub
- URL: https://github.com/vzhn/netty-http-authenticator
- Owner: vzhn
- License: mit
- Created: 2019-05-01T11:14:43.000Z (almost 7 years ago)
- Default Branch: master
- Last Pushed: 2023-04-18T22:20:56.000Z (almost 3 years ago)
- Last Synced: 2025-07-20T07:41:20.881Z (9 months ago)
- Topics: digest-authentication, http, netty, rtsp
- Language: Java
- Homepage:
- Size: 202 KB
- Stars: 12
- Watchers: 2
- Forks: 8
- Open Issues: 3
-
Metadata Files:
- Readme: readme.md
- License: LICENSE.md
Awesome Lists containing this project
README


### BasicNetyHttpAuthenticator
`BasicNettyHttpAuthenticator` just appends the auth header to an every request.
```java
Bootstrap b = new Bootstrap();
b.group(group)
.channel(NioSocketChannel.class)
.handler(new ChannelInitializer() {
@Override
protected void initChannel(SocketChannel ch) {
ChannelPipeline p = ch.pipeline();
p.addLast(new HttpClientCodec());
p.addLast(new HttpObjectAggregator(1048576));
p.addLast(new BasicNettyHttpAuthenticator("scott", "tiger"));
p.addLast(new HttpClientHandler());
}
});
```
### DigestNettyHttpAuthenticator
```java
DigestAuthenticator digestAuthenticator = new DigestAuthenticator("scott", "tiger");
Bootstrap b = new Bootstrap();
b.group(group)
.channel(NioSocketChannel.class)
.handler(new ChannelInitializer() {
@Override
protected void initChannel(SocketChannel ch) {
ChannelPipeline p = ch.pipeline();
p.addLast(new HttpClientCodec());
p.addLast(new DigestNettyHttpAuthenticator(digestAuthenticator));
p.addLast(new HttpClientHandler());
}
});
...
ch.writeAndFlush(firstSequest); // the first request got 401 error
ch.writeAndFlush(secondRequest); // the second will succeeded if credentinals are not wrong
```
### TransparentDigestNettyHttpAuthenticator
This is the tricky one. It works only with aggregated HTTP messages: `FullHttpRequest` and `FullHttpResponse` and keep-alive connection.
The solution that fits for `RTSP`.
`TransparentDigestNettyHttpAuthenticator` must be initialized with `username` and `password` and placed in a channel pipeline between `HttpObjectAggregator`
and handler that processes server responses, like this:
```java
DigestAuthenticator digestAuthenticator = new DigestAuthenticator("scott", "tiger");
Bootstrap b = new Bootstrap();
b.group(group)
.channel(NioSocketChannel.class)
.handler(new ChannelInitializer() {
@Override
protected void initChannel(SocketChannel ch) {
ChannelPipeline p = ch.pipeline();
p.addLast(new HttpClientCodec());
p.addLast(new HttpObjectAggregator(1048576)); // NB! works only with aggregated request/response
p.addLast(new TransparentDigestNettyHttpAuthenticator(authenticator));
p.addLast(new HttpClientHandler());
}
});
..
ch.writeAndFlush(request); // the first attempt will succeeded if credentinals are not wrong
```
##### How it works
`TransparentDigestNettyHttpAuthenticator` intercepts the client request, and remembers it
* If a server returns the `401 Unathorized` error, authenticator resends the request with proper authorization header
* If a server returns `200 OK`, authenticator attaches the authorization header to all subsequent requests
* If a server returns the `401 Unathorized` error again, and `stale=false`, authenticator pass that *error* to client (bad credentials)
* If a server returns the `401 Unathorized error` and `stale=true`, authenticator generates a new client nonce and resend the request with new authorization header
Typical client-server exchange may look like this:

## Downloading from the Maven central repository
Add the following dependency section to your pom.xml:
```xml
io.github.vzhn
netty-http-authenticator
1.1
```
## How to contribute
Make your changes, and submit a pull request. Contributions are welcome!
## License
This project is licensed under the MIT License - see the [LICENSE.md](LICENSE.md) file for details