1package com.android.anqp.eap;
2
3import java.net.ProtocolException;
4import java.nio.ByteBuffer;
5import java.util.EnumMap;
6import java.util.HashMap;
7import java.util.Map;
8
9import static com.android.anqp.Constants.BYTE_MASK;
10
11/**
12 * An EAP authentication parameter, IEEE802.11-2012, table 8-188
13 */
14public class NonEAPInnerAuth implements AuthParam {
15
16    public enum NonEAPType {Reserved, PAP, CHAP, MSCHAP, MSCHAPv2}
17    private static final Map<NonEAPType, String> sOmaMap = new EnumMap<>(NonEAPType.class);
18    private static final Map<String, NonEAPType> sRevOmaMap = new HashMap<>();
19
20    private final NonEAPType mType;
21
22    static {
23        sOmaMap.put(NonEAPType.PAP, "PAP");
24        sOmaMap.put(NonEAPType.CHAP, "CHAP");
25        sOmaMap.put(NonEAPType.MSCHAP, "MS-CHAP");
26        sOmaMap.put(NonEAPType.MSCHAPv2, "MS-CHAP-V2");
27
28        for (Map.Entry<NonEAPType, String> entry : sOmaMap.entrySet()) {
29            sRevOmaMap.put(entry.getValue(), entry.getKey());
30        }
31    }
32
33    public NonEAPInnerAuth(int length, ByteBuffer payload) throws ProtocolException {
34        if (length != 1) {
35            throw new ProtocolException("Bad length: " + payload.remaining());
36        }
37
38        int typeID = payload.get() & BYTE_MASK;
39        mType = typeID < NonEAPType.values().length ?
40                NonEAPType.values()[typeID] :
41                NonEAPType.Reserved;
42    }
43
44    public NonEAPInnerAuth(NonEAPType type) {
45        mType = type;
46    }
47
48    /**
49     * Construct from the OMA-DM PPS data
50     * @param eapType as defined in the HS2.0 spec.
51     */
52    public NonEAPInnerAuth(String eapType) {
53        mType = sRevOmaMap.get(eapType);
54    }
55
56    @Override
57    public EAP.AuthInfoID getAuthInfoID() {
58        return EAP.AuthInfoID.NonEAPInnerAuthType;
59    }
60
61    public NonEAPType getType() {
62        return mType;
63    }
64
65    public String getOMAtype() {
66        return sOmaMap.get(mType);
67    }
68
69    public static String mapInnerType(NonEAPType type) {
70        return sOmaMap.get(type);
71    }
72
73    @Override
74    public int hashCode() {
75        return mType.hashCode();
76    }
77
78    @Override
79    public boolean equals(Object thatObject) {
80        if (thatObject == this) {
81            return true;
82        } else if (thatObject == null || thatObject.getClass() != NonEAPInnerAuth.class) {
83            return false;
84        } else {
85            return ((NonEAPInnerAuth) thatObject).getType() == getType();
86        }
87    }
88
89    @Override
90    public String toString() {
91        return "Auth method NonEAPInnerAuthEAP, inner = " + mType + '\n';
92    }
93}
94