View Javadoc

1   /*
2    * Copyright 2012 The Netty Project
3    *
4    * The Netty Project licenses this file to you under the Apache License,
5    * version 2.0 (the "License"); you may not use this file except in compliance
6    * with the License. You may obtain a copy of the License at:
7    *
8    *   http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13   * License for the specific language governing permissions and limitations
14   * under the License.
15   */
16  package io.netty.handler.codec.socks;
17  
18  import io.netty.buffer.ByteBuf;
19  import io.netty.util.CharsetUtil;
20  
21  import java.nio.charset.CharsetEncoder;
22  
23  /**
24   * An socks auth request.
25   *
26   * @see SocksAuthResponse
27   * @see SocksAuthRequestDecoder
28   */
29  public final class SocksAuthRequest extends SocksRequest {
30      private static final CharsetEncoder asciiEncoder = CharsetUtil.encoder(CharsetUtil.US_ASCII);
31      private static final SocksSubnegotiationVersion SUBNEGOTIATION_VERSION = SocksSubnegotiationVersion.AUTH_PASSWORD;
32      private final String username;
33      private final String password;
34  
35      public SocksAuthRequest(String username, String password) {
36          super(SocksRequestType.AUTH);
37          if (username == null) {
38              throw new NullPointerException("username");
39          }
40          if (password == null) {
41              throw new NullPointerException("username");
42          }
43          if (!asciiEncoder.canEncode(username) || !asciiEncoder.canEncode(password)) {
44              throw new IllegalArgumentException(
45                      "username: " + username + " or password: **** values should be in pure ascii");
46          }
47          if (username.length() > 255) {
48              throw new IllegalArgumentException("username: " + username + " exceeds 255 char limit");
49          }
50          if (password.length() > 255) {
51              throw new IllegalArgumentException("password: **** exceeds 255 char limit");
52          }
53          this.username = username;
54          this.password = password;
55      }
56  
57      /**
58       * Returns username that needs to be authenticated
59       *
60       * @return username that needs to be authenticated
61       */
62      public String username() {
63          return username;
64      }
65  
66      /**
67       * Returns password that needs to be validated
68       *
69       * @return password that needs to be validated
70       */
71      public String password() {
72          return password;
73      }
74  
75      @Override
76      public void encodeAsByteBuf(ByteBuf byteBuf) {
77          byteBuf.writeByte(SUBNEGOTIATION_VERSION.byteValue());
78          byteBuf.writeByte(username.length());
79          byteBuf.writeBytes(username.getBytes(CharsetUtil.US_ASCII));
80          byteBuf.writeByte(password.length());
81          byteBuf.writeBytes(password.getBytes(CharsetUtil.US_ASCII));
82      }
83  }