Ari Saha | 8983174 | 2015-06-26 10:31:48 -0700 | [diff] [blame] | 1 | /* |
Amit Ghosh | c9ac1e5 | 2017-07-28 12:31:18 +0100 | [diff] [blame] | 2 | * Copyright 2017-present Open Networking Foundation |
Ari Saha | 8983174 | 2015-06-26 10:31:48 -0700 | [diff] [blame] | 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * 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, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | * |
| 16 | */ |
| 17 | |
alshabib | 6d52745 | 2016-06-01 18:00:47 -0700 | [diff] [blame] | 18 | package org.opencord.aaa; |
Ari Saha | 8983174 | 2015-06-26 10:31:48 -0700 | [diff] [blame] | 19 | |
Jonathan Hart | 4731dd9 | 2018-05-02 17:30:05 -0700 | [diff] [blame] | 20 | import com.google.common.collect.Maps; |
Ari Saha | 8983174 | 2015-06-26 10:31:48 -0700 | [diff] [blame] | 21 | import org.onlab.packet.MacAddress; |
| 22 | import org.onosproject.net.ConnectPoint; |
Ari Saha | 8983174 | 2015-06-26 10:31:48 -0700 | [diff] [blame] | 23 | import org.slf4j.Logger; |
Thomas Vachuska | e989420 | 2015-07-30 11:59:07 -0700 | [diff] [blame] | 24 | |
Jonathan Hart | 4731dd9 | 2018-05-02 17:30:05 -0700 | [diff] [blame] | 25 | import java.util.Map; |
Ari Saha | 8983174 | 2015-06-26 10:31:48 -0700 | [diff] [blame] | 26 | |
| 27 | import static org.slf4j.LoggerFactory.getLogger; |
| 28 | |
| 29 | /** |
| 30 | * AAA Finite State Machine. |
| 31 | */ |
| 32 | |
| 33 | class StateMachine { |
| 34 | //INDEX to identify the state in the transition table |
| 35 | static final int STATE_IDLE = 0; |
| 36 | static final int STATE_STARTED = 1; |
| 37 | static final int STATE_PENDING = 2; |
| 38 | static final int STATE_AUTHORIZED = 3; |
| 39 | static final int STATE_UNAUTHORIZED = 4; |
| 40 | |
| 41 | //INDEX to identify the transition in the transition table |
| 42 | static final int TRANSITION_START = 0; // --> started |
| 43 | static final int TRANSITION_REQUEST_ACCESS = 1; |
| 44 | static final int TRANSITION_AUTHORIZE_ACCESS = 2; |
| 45 | static final int TRANSITION_DENY_ACCESS = 3; |
| 46 | static final int TRANSITION_LOGOFF = 4; |
| 47 | |
Amit Ghosh | c9ac1e5 | 2017-07-28 12:31:18 +0100 | [diff] [blame] | 48 | private static int identifier = -1; |
Ari Saha | 8983174 | 2015-06-26 10:31:48 -0700 | [diff] [blame] | 49 | private byte challengeIdentifier; |
| 50 | private byte[] challengeState; |
| 51 | private byte[] username; |
| 52 | private byte[] requestAuthenticator; |
| 53 | |
| 54 | // Supplicant connectivity info |
Ray Milkey | f61a24e | 2015-09-24 16:34:02 -0700 | [diff] [blame] | 55 | private ConnectPoint supplicantConnectpoint; |
| 56 | private MacAddress supplicantAddress; |
| 57 | private short vlanId; |
Amit Ghosh | c9ac1e5 | 2017-07-28 12:31:18 +0100 | [diff] [blame] | 58 | private byte priorityCode; |
Ari Saha | 8983174 | 2015-06-26 10:31:48 -0700 | [diff] [blame] | 59 | |
| 60 | private String sessionId = null; |
| 61 | |
| 62 | private final Logger log = getLogger(getClass()); |
| 63 | |
| 64 | |
| 65 | private State[] states = { |
| 66 | new Idle(), new Started(), new Pending(), new Authorized(), new Unauthorized() |
| 67 | }; |
| 68 | |
| 69 | |
| 70 | //State transition table |
| 71 | /* |
| 72 | |
| 73 | state IDLE | STARTED | PENDING | AUTHORIZED | UNAUTHORIZED |
| 74 | //// |
| 75 | input |
| 76 | ---------------------------------------------------------------------------------------------------- |
| 77 | |
ke han | 04e47f3 | 2016-10-28 14:15:43 +0800 | [diff] [blame] | 78 | START STARTED | _ | _ | STARTED | STARTED |
Ari Saha | 8983174 | 2015-06-26 10:31:48 -0700 | [diff] [blame] | 79 | |
| 80 | REQUEST_ACCESS _ | PENDING | _ | _ | _ |
| 81 | |
| 82 | AUTHORIZE_ACCESS _ | _ | AUTHORIZED | _ | _ |
| 83 | |
| 84 | DENY_ACCESS _ | - | UNAUTHORIZED | _ | _ |
| 85 | |
| 86 | LOGOFF _ | _ | _ | IDLE | IDLE |
| 87 | */ |
| 88 | |
| 89 | private int[] idleTransition = |
| 90 | {STATE_STARTED, STATE_IDLE, STATE_IDLE, STATE_IDLE, STATE_IDLE}; |
| 91 | private int[] startedTransition = |
| 92 | {STATE_STARTED, STATE_PENDING, STATE_STARTED, STATE_STARTED, STATE_STARTED}; |
| 93 | private int[] pendingTransition = |
| 94 | {STATE_PENDING, STATE_PENDING, STATE_AUTHORIZED, STATE_UNAUTHORIZED, STATE_PENDING}; |
| 95 | private int[] authorizedTransition = |
Qianqian Hu | 0c34981 | 2016-02-15 17:25:22 +0800 | [diff] [blame] | 96 | {STATE_STARTED, STATE_AUTHORIZED, STATE_AUTHORIZED, STATE_AUTHORIZED, STATE_IDLE}; |
Ari Saha | 8983174 | 2015-06-26 10:31:48 -0700 | [diff] [blame] | 97 | private int[] unauthorizedTransition = |
ke han | 04e47f3 | 2016-10-28 14:15:43 +0800 | [diff] [blame] | 98 | {STATE_STARTED, STATE_UNAUTHORIZED, STATE_UNAUTHORIZED, STATE_UNAUTHORIZED, STATE_IDLE}; |
Ari Saha | 8983174 | 2015-06-26 10:31:48 -0700 | [diff] [blame] | 99 | |
| 100 | //THE TRANSITION TABLE |
| 101 | private int[][] transition = |
| 102 | {idleTransition, startedTransition, pendingTransition, authorizedTransition, |
| 103 | unauthorizedTransition}; |
| 104 | |
| 105 | private int currentState = STATE_IDLE; |
| 106 | |
Ray Milkey | f61a24e | 2015-09-24 16:34:02 -0700 | [diff] [blame] | 107 | // Maps of state machines. Each state machine is represented by an |
| 108 | // unique identifier on the switch: dpid + port number |
| 109 | private static Map<String, StateMachine> sessionIdMap; |
| 110 | private static Map<Integer, StateMachine> identifierMap; |
Ari Saha | 8983174 | 2015-06-26 10:31:48 -0700 | [diff] [blame] | 111 | |
Ray Milkey | f61a24e | 2015-09-24 16:34:02 -0700 | [diff] [blame] | 112 | public static void initializeMaps() { |
| 113 | sessionIdMap = Maps.newConcurrentMap(); |
| 114 | identifierMap = Maps.newConcurrentMap(); |
Amit Ghosh | c9ac1e5 | 2017-07-28 12:31:18 +0100 | [diff] [blame] | 115 | identifier = -1; |
Ray Milkey | f61a24e | 2015-09-24 16:34:02 -0700 | [diff] [blame] | 116 | } |
| 117 | |
| 118 | public static void destroyMaps() { |
| 119 | sessionIdMap = null; |
| 120 | identifierMap = null; |
| 121 | } |
| 122 | |
Qianqian Hu | 61a6a40 | 2016-02-16 15:18:05 +0800 | [diff] [blame] | 123 | public static Map<String, StateMachine> sessionIdMap() { |
| 124 | return sessionIdMap; |
| 125 | } |
| 126 | |
Ray Milkey | f61a24e | 2015-09-24 16:34:02 -0700 | [diff] [blame] | 127 | public static StateMachine lookupStateMachineById(byte identifier) { |
| 128 | return identifierMap.get((int) identifier); |
| 129 | } |
| 130 | |
| 131 | public static StateMachine lookupStateMachineBySessionId(String sessionId) { |
| 132 | return sessionIdMap.get(sessionId); |
Amit Ghosh | c9ac1e5 | 2017-07-28 12:31:18 +0100 | [diff] [blame] | 133 | } |
| 134 | |
| 135 | public static void deleteStateMachineMapping(StateMachine machine) { |
| 136 | identifierMap.entrySet().removeIf(e -> e.getValue().equals(machine)); |
| 137 | } |
| 138 | |
| 139 | /** |
David K. Bainbridge | 6201949 | 2017-07-28 17:02:10 -0700 | [diff] [blame] | 140 | * Deletes authentication state machine records for a given MAC address. |
| 141 | * @param mac mac address of the suppliant who's state machine should be removed |
| 142 | */ |
| 143 | public static void deleteByMac(MacAddress mac) { |
| 144 | |
| 145 | // Walk the map from session IDs to state machines looking for a MAC match |
| 146 | for (Map.Entry<String, StateMachine> e : sessionIdMap.entrySet()) { |
| 147 | |
| 148 | // If a MAC match is found then delete the entry from the session ID |
| 149 | // and identifier map as well as call delete identifier to clean up |
| 150 | // the identifier bit set. |
| 151 | if (e.getValue() != null && e.getValue().supplicantAddress != null && |
| 152 | e.getValue().supplicantAddress.equals(mac)) { |
| 153 | sessionIdMap.remove(e.getValue().sessionId); |
| 154 | if (e.getValue().identifier != -1) { |
| 155 | deleteStateMachineMapping(e.getValue()); |
| 156 | } |
| 157 | break; |
| 158 | } |
| 159 | } |
| 160 | } |
| 161 | |
| 162 | /** |
Jonathan Hart | 932bedc | 2018-07-12 13:46:09 -0700 | [diff] [blame^] | 163 | * Creates a new StateMachine with the given session ID. |
Thomas Vachuska | e989420 | 2015-07-30 11:59:07 -0700 | [diff] [blame] | 164 | * |
Jonathan Hart | 932bedc | 2018-07-12 13:46:09 -0700 | [diff] [blame^] | 165 | * @param sessionId session Id represented by the switch dpid + port number |
Ari Saha | 8983174 | 2015-06-26 10:31:48 -0700 | [diff] [blame] | 166 | */ |
Jonathan Hart | 932bedc | 2018-07-12 13:46:09 -0700 | [diff] [blame^] | 167 | public StateMachine(String sessionId) { |
| 168 | log.info("Creating a new state machine for {}", sessionId); |
Ari Saha | 8983174 | 2015-06-26 10:31:48 -0700 | [diff] [blame] | 169 | this.sessionId = sessionId; |
Ray Milkey | f61a24e | 2015-09-24 16:34:02 -0700 | [diff] [blame] | 170 | sessionIdMap.put(sessionId, this); |
Ari Saha | 8983174 | 2015-06-26 10:31:48 -0700 | [diff] [blame] | 171 | } |
| 172 | |
| 173 | /** |
Ray Milkey | f61a24e | 2015-09-24 16:34:02 -0700 | [diff] [blame] | 174 | * Gets the connect point for the supplicant side. |
| 175 | * |
| 176 | * @return supplicant connect point |
| 177 | */ |
| 178 | public ConnectPoint supplicantConnectpoint() { |
| 179 | return supplicantConnectpoint; |
| 180 | } |
| 181 | |
| 182 | /** |
| 183 | * Sets the supplicant side connect point. |
| 184 | * |
| 185 | * @param supplicantConnectpoint supplicant select point. |
| 186 | */ |
| 187 | public void setSupplicantConnectpoint(ConnectPoint supplicantConnectpoint) { |
| 188 | this.supplicantConnectpoint = supplicantConnectpoint; |
| 189 | } |
| 190 | |
| 191 | /** |
| 192 | * Gets the MAC address of the supplicant. |
| 193 | * |
| 194 | * @return supplicant MAC address |
| 195 | */ |
| 196 | public MacAddress supplicantAddress() { |
| 197 | return supplicantAddress; |
| 198 | } |
| 199 | |
| 200 | /** |
| 201 | * Sets the supplicant MAC address. |
| 202 | * |
| 203 | * @param supplicantAddress new supplicant MAC address |
| 204 | */ |
| 205 | public void setSupplicantAddress(MacAddress supplicantAddress) { |
| 206 | this.supplicantAddress = supplicantAddress; |
| 207 | } |
| 208 | |
| 209 | /** |
| 210 | * Gets the client's Vlan ID. |
| 211 | * |
| 212 | * @return client vlan ID |
| 213 | */ |
| 214 | public short vlanId() { |
| 215 | return vlanId; |
| 216 | } |
| 217 | |
| 218 | /** |
| 219 | * Sets the client's vlan ID. |
| 220 | * |
| 221 | * @param vlanId new client vlan ID |
| 222 | */ |
| 223 | public void setVlanId(short vlanId) { |
| 224 | this.vlanId = vlanId; |
| 225 | } |
| 226 | |
| 227 | /** |
Amit Ghosh | c9ac1e5 | 2017-07-28 12:31:18 +0100 | [diff] [blame] | 228 | * Gets the client's priority Code. |
| 229 | * |
| 230 | * @return client Priority code |
| 231 | */ |
| 232 | public byte priorityCode() { |
| 233 | return priorityCode; |
| 234 | } |
| 235 | |
| 236 | /** |
| 237 | * Sets the client's priority Code. |
| 238 | * |
| 239 | * @param priorityCode new client priority Code |
| 240 | */ |
| 241 | public void setPriorityCode(byte priorityCode) { |
| 242 | this.priorityCode = priorityCode; |
| 243 | } |
| 244 | |
| 245 | /** |
Ray Milkey | f61a24e | 2015-09-24 16:34:02 -0700 | [diff] [blame] | 246 | * Gets the client id that is requesting for access. |
Thomas Vachuska | e989420 | 2015-07-30 11:59:07 -0700 | [diff] [blame] | 247 | * |
Ari Saha | 8983174 | 2015-06-26 10:31:48 -0700 | [diff] [blame] | 248 | * @return The client id. |
| 249 | */ |
Ray Milkey | f61a24e | 2015-09-24 16:34:02 -0700 | [diff] [blame] | 250 | public String sessionId() { |
Ari Saha | 8983174 | 2015-06-26 10:31:48 -0700 | [diff] [blame] | 251 | return this.sessionId; |
| 252 | } |
| 253 | |
| 254 | /** |
Ari Saha | 8983174 | 2015-06-26 10:31:48 -0700 | [diff] [blame] | 255 | * Set the challenge identifier and the state issued by the RADIUS. |
Thomas Vachuska | e989420 | 2015-07-30 11:59:07 -0700 | [diff] [blame] | 256 | * |
Ari Saha | 8983174 | 2015-06-26 10:31:48 -0700 | [diff] [blame] | 257 | * @param challengeIdentifier The challenge identifier set into the EAP packet from the RADIUS message. |
Thomas Vachuska | e989420 | 2015-07-30 11:59:07 -0700 | [diff] [blame] | 258 | * @param challengeState The challenge state from the RADIUS. |
Ari Saha | 8983174 | 2015-06-26 10:31:48 -0700 | [diff] [blame] | 259 | */ |
| 260 | protected void setChallengeInfo(byte challengeIdentifier, byte[] challengeState) { |
| 261 | this.challengeIdentifier = challengeIdentifier; |
| 262 | this.challengeState = challengeState; |
| 263 | } |
Thomas Vachuska | e989420 | 2015-07-30 11:59:07 -0700 | [diff] [blame] | 264 | |
Ari Saha | 8983174 | 2015-06-26 10:31:48 -0700 | [diff] [blame] | 265 | /** |
| 266 | * Set the challenge identifier issued by the RADIUS on the access challenge request. |
Thomas Vachuska | e989420 | 2015-07-30 11:59:07 -0700 | [diff] [blame] | 267 | * |
Ari Saha | 8983174 | 2015-06-26 10:31:48 -0700 | [diff] [blame] | 268 | * @param challengeIdentifier The challenge identifier set into the EAP packet from the RADIUS message. |
| 269 | */ |
| 270 | protected void setChallengeIdentifier(byte challengeIdentifier) { |
| 271 | log.info("Set Challenge Identifier to {}", challengeIdentifier); |
| 272 | this.challengeIdentifier = challengeIdentifier; |
| 273 | } |
| 274 | |
| 275 | /** |
Ray Milkey | f61a24e | 2015-09-24 16:34:02 -0700 | [diff] [blame] | 276 | * Gets the challenge EAP identifier set by the RADIUS. |
Thomas Vachuska | e989420 | 2015-07-30 11:59:07 -0700 | [diff] [blame] | 277 | * |
Ari Saha | 8983174 | 2015-06-26 10:31:48 -0700 | [diff] [blame] | 278 | * @return The challenge EAP identifier. |
| 279 | */ |
Ray Milkey | f61a24e | 2015-09-24 16:34:02 -0700 | [diff] [blame] | 280 | protected byte challengeIdentifier() { |
Ari Saha | 8983174 | 2015-06-26 10:31:48 -0700 | [diff] [blame] | 281 | return this.challengeIdentifier; |
| 282 | } |
| 283 | |
| 284 | |
| 285 | /** |
| 286 | * Set the challenge state info issued by the RADIUS. |
Thomas Vachuska | e989420 | 2015-07-30 11:59:07 -0700 | [diff] [blame] | 287 | * |
Ari Saha | 8983174 | 2015-06-26 10:31:48 -0700 | [diff] [blame] | 288 | * @param challengeState The challenge state from the RADIUS. |
| 289 | */ |
| 290 | protected void setChallengeState(byte[] challengeState) { |
| 291 | log.info("Set Challenge State"); |
| 292 | this.challengeState = challengeState; |
| 293 | } |
| 294 | |
| 295 | /** |
Ray Milkey | f61a24e | 2015-09-24 16:34:02 -0700 | [diff] [blame] | 296 | * Gets the challenge state set by the RADIUS. |
Thomas Vachuska | e989420 | 2015-07-30 11:59:07 -0700 | [diff] [blame] | 297 | * |
Ari Saha | 8983174 | 2015-06-26 10:31:48 -0700 | [diff] [blame] | 298 | * @return The challenge state. |
| 299 | */ |
Ray Milkey | f61a24e | 2015-09-24 16:34:02 -0700 | [diff] [blame] | 300 | protected byte[] challengeState() { |
Ari Saha | 8983174 | 2015-06-26 10:31:48 -0700 | [diff] [blame] | 301 | return this.challengeState; |
| 302 | } |
| 303 | |
| 304 | /** |
| 305 | * Set the username. |
Thomas Vachuska | e989420 | 2015-07-30 11:59:07 -0700 | [diff] [blame] | 306 | * |
Ari Saha | 8983174 | 2015-06-26 10:31:48 -0700 | [diff] [blame] | 307 | * @param username The username sent to the RADIUS upon access request. |
| 308 | */ |
| 309 | protected void setUsername(byte[] username) { |
| 310 | this.username = username; |
| 311 | } |
| 312 | |
Ari Saha | 8983174 | 2015-06-26 10:31:48 -0700 | [diff] [blame] | 313 | /** |
Ray Milkey | f61a24e | 2015-09-24 16:34:02 -0700 | [diff] [blame] | 314 | * Gets the username. |
Thomas Vachuska | e989420 | 2015-07-30 11:59:07 -0700 | [diff] [blame] | 315 | * |
Ari Saha | 8983174 | 2015-06-26 10:31:48 -0700 | [diff] [blame] | 316 | * @return The requestAuthenticator. |
| 317 | */ |
Ray Milkey | f61a24e | 2015-09-24 16:34:02 -0700 | [diff] [blame] | 318 | protected byte[] requestAuthenticator() { |
Ari Saha | 8983174 | 2015-06-26 10:31:48 -0700 | [diff] [blame] | 319 | return this.requestAuthenticator; |
| 320 | } |
| 321 | |
| 322 | /** |
Ray Milkey | f61a24e | 2015-09-24 16:34:02 -0700 | [diff] [blame] | 323 | * Sets the authenticator. |
Thomas Vachuska | e989420 | 2015-07-30 11:59:07 -0700 | [diff] [blame] | 324 | * |
Ari Saha | 8983174 | 2015-06-26 10:31:48 -0700 | [diff] [blame] | 325 | * @param authenticator The username sent to the RADIUS upon access request. |
| 326 | */ |
| 327 | protected void setRequestAuthenticator(byte[] authenticator) { |
| 328 | this.requestAuthenticator = authenticator; |
| 329 | } |
| 330 | |
| 331 | |
| 332 | /** |
Ray Milkey | f61a24e | 2015-09-24 16:34:02 -0700 | [diff] [blame] | 333 | * Gets the username. |
Thomas Vachuska | e989420 | 2015-07-30 11:59:07 -0700 | [diff] [blame] | 334 | * |
Ari Saha | 8983174 | 2015-06-26 10:31:48 -0700 | [diff] [blame] | 335 | * @return The username. |
| 336 | */ |
Ray Milkey | f61a24e | 2015-09-24 16:34:02 -0700 | [diff] [blame] | 337 | protected byte[] username() { |
Ari Saha | 8983174 | 2015-06-26 10:31:48 -0700 | [diff] [blame] | 338 | return this.username; |
| 339 | } |
| 340 | |
| 341 | /** |
| 342 | * Return the identifier of the state machine. |
Thomas Vachuska | e989420 | 2015-07-30 11:59:07 -0700 | [diff] [blame] | 343 | * |
Ari Saha | 8983174 | 2015-06-26 10:31:48 -0700 | [diff] [blame] | 344 | * @return The state machine identifier. |
| 345 | */ |
Amit Ghosh | c9ac1e5 | 2017-07-28 12:31:18 +0100 | [diff] [blame] | 346 | public synchronized byte identifier() { |
| 347 | identifier = (identifier + 1) % 255; |
| 348 | identifierMap.put(identifier, this); |
| 349 | return (byte) identifier; |
Ari Saha | 8983174 | 2015-06-26 10:31:48 -0700 | [diff] [blame] | 350 | } |
| 351 | |
Ari Saha | 8983174 | 2015-06-26 10:31:48 -0700 | [diff] [blame] | 352 | /** |
| 353 | * Move to the next state. |
Thomas Vachuska | e989420 | 2015-07-30 11:59:07 -0700 | [diff] [blame] | 354 | * |
Ray Milkey | 78e95a4 | 2015-09-24 08:36:45 -0700 | [diff] [blame] | 355 | * @param msg message |
Ari Saha | 8983174 | 2015-06-26 10:31:48 -0700 | [diff] [blame] | 356 | */ |
Thomas Vachuska | e989420 | 2015-07-30 11:59:07 -0700 | [diff] [blame] | 357 | private void next(int msg) { |
Ari Saha | 8983174 | 2015-06-26 10:31:48 -0700 | [diff] [blame] | 358 | currentState = transition[currentState][msg]; |
| 359 | log.info("Current State " + currentState); |
| 360 | } |
| 361 | |
| 362 | /** |
| 363 | * Client has requested the start action to allow network access. |
Thomas Vachuska | e989420 | 2015-07-30 11:59:07 -0700 | [diff] [blame] | 364 | * |
| 365 | * @throws StateMachineException if authentication protocol is violated |
Ari Saha | 8983174 | 2015-06-26 10:31:48 -0700 | [diff] [blame] | 366 | */ |
| 367 | public void start() throws StateMachineException { |
Ray Milkey | 78e95a4 | 2015-09-24 08:36:45 -0700 | [diff] [blame] | 368 | states[currentState].start(); |
| 369 | //move to the next state |
| 370 | next(TRANSITION_START); |
Amit Ghosh | c9ac1e5 | 2017-07-28 12:31:18 +0100 | [diff] [blame] | 371 | identifier = this.identifier(); |
Ari Saha | 8983174 | 2015-06-26 10:31:48 -0700 | [diff] [blame] | 372 | } |
| 373 | |
| 374 | /** |
| 375 | * An Identification information has been sent by the supplicant. |
| 376 | * Move to the next state if possible. |
Thomas Vachuska | e989420 | 2015-07-30 11:59:07 -0700 | [diff] [blame] | 377 | * |
| 378 | * @throws StateMachineException if authentication protocol is violated |
Ari Saha | 8983174 | 2015-06-26 10:31:48 -0700 | [diff] [blame] | 379 | */ |
| 380 | public void requestAccess() throws StateMachineException { |
Ray Milkey | 78e95a4 | 2015-09-24 08:36:45 -0700 | [diff] [blame] | 381 | states[currentState].requestAccess(); |
| 382 | //move to the next state |
| 383 | next(TRANSITION_REQUEST_ACCESS); |
Ari Saha | 8983174 | 2015-06-26 10:31:48 -0700 | [diff] [blame] | 384 | } |
| 385 | |
| 386 | /** |
| 387 | * RADIUS has accepted the identification. |
| 388 | * Move to the next state if possible. |
Thomas Vachuska | e989420 | 2015-07-30 11:59:07 -0700 | [diff] [blame] | 389 | * |
| 390 | * @throws StateMachineException if authentication protocol is violated |
Ari Saha | 8983174 | 2015-06-26 10:31:48 -0700 | [diff] [blame] | 391 | */ |
| 392 | public void authorizeAccess() throws StateMachineException { |
Ray Milkey | 78e95a4 | 2015-09-24 08:36:45 -0700 | [diff] [blame] | 393 | states[currentState].radiusAccepted(); |
| 394 | //move to the next state |
| 395 | next(TRANSITION_AUTHORIZE_ACCESS); |
Ari Saha | 8983174 | 2015-06-26 10:31:48 -0700 | [diff] [blame] | 396 | |
Jonathan Hart | 932bedc | 2018-07-12 13:46:09 -0700 | [diff] [blame^] | 397 | // TODO send state machine change event |
Ari Saha | 8983174 | 2015-06-26 10:31:48 -0700 | [diff] [blame] | 398 | |
Amit Ghosh | c9ac1e5 | 2017-07-28 12:31:18 +0100 | [diff] [blame] | 399 | // Clear mapping |
| 400 | deleteStateMachineMapping(this); |
Ari Saha | 8983174 | 2015-06-26 10:31:48 -0700 | [diff] [blame] | 401 | } |
| 402 | |
| 403 | /** |
| 404 | * RADIUS has denied the identification. |
| 405 | * Move to the next state if possible. |
Thomas Vachuska | e989420 | 2015-07-30 11:59:07 -0700 | [diff] [blame] | 406 | * |
| 407 | * @throws StateMachineException if authentication protocol is violated |
Ari Saha | 8983174 | 2015-06-26 10:31:48 -0700 | [diff] [blame] | 408 | */ |
| 409 | public void denyAccess() throws StateMachineException { |
Ray Milkey | 78e95a4 | 2015-09-24 08:36:45 -0700 | [diff] [blame] | 410 | states[currentState].radiusDenied(); |
| 411 | //move to the next state |
| 412 | next(TRANSITION_DENY_ACCESS); |
Amit Ghosh | c9ac1e5 | 2017-07-28 12:31:18 +0100 | [diff] [blame] | 413 | // Clear mappings |
| 414 | deleteStateMachineMapping(this); |
Ari Saha | 8983174 | 2015-06-26 10:31:48 -0700 | [diff] [blame] | 415 | } |
| 416 | |
| 417 | /** |
| 418 | * Logoff request has been requested. |
| 419 | * Move to the next state if possible. |
Thomas Vachuska | e989420 | 2015-07-30 11:59:07 -0700 | [diff] [blame] | 420 | * |
| 421 | * @throws StateMachineException if authentication protocol is violated |
Ari Saha | 8983174 | 2015-06-26 10:31:48 -0700 | [diff] [blame] | 422 | */ |
| 423 | public void logoff() throws StateMachineException { |
Ray Milkey | 78e95a4 | 2015-09-24 08:36:45 -0700 | [diff] [blame] | 424 | states[currentState].logoff(); |
| 425 | //move to the next state |
| 426 | next(TRANSITION_LOGOFF); |
Ari Saha | 8983174 | 2015-06-26 10:31:48 -0700 | [diff] [blame] | 427 | } |
| 428 | |
| 429 | /** |
Ray Milkey | f61a24e | 2015-09-24 16:34:02 -0700 | [diff] [blame] | 430 | * Gets the current state. |
Thomas Vachuska | e989420 | 2015-07-30 11:59:07 -0700 | [diff] [blame] | 431 | * |
Ari Saha | 8983174 | 2015-06-26 10:31:48 -0700 | [diff] [blame] | 432 | * @return The current state. Could be STATE_IDLE, STATE_STARTED, STATE_PENDING, STATE_AUTHORIZED, |
| 433 | * STATE_UNAUTHORIZED. |
| 434 | */ |
Ray Milkey | f61a24e | 2015-09-24 16:34:02 -0700 | [diff] [blame] | 435 | public int state() { |
Ari Saha | 8983174 | 2015-06-26 10:31:48 -0700 | [diff] [blame] | 436 | return currentState; |
| 437 | } |
| 438 | |
Ray Milkey | f61a24e | 2015-09-24 16:34:02 -0700 | [diff] [blame] | 439 | @Override |
Ari Saha | 8983174 | 2015-06-26 10:31:48 -0700 | [diff] [blame] | 440 | public String toString() { |
| 441 | return ("sessionId: " + this.sessionId) + "\t" + ("identifier: " + this.identifier) + "\t" + |
| 442 | ("state: " + this.currentState); |
| 443 | } |
Ari Saha | 8983174 | 2015-06-26 10:31:48 -0700 | [diff] [blame] | 444 | |
Ray Milkey | 78e95a4 | 2015-09-24 08:36:45 -0700 | [diff] [blame] | 445 | abstract class State { |
| 446 | private final Logger log = getLogger(getClass()); |
Thomas Vachuska | e989420 | 2015-07-30 11:59:07 -0700 | [diff] [blame] | 447 | |
Ray Milkey | 78e95a4 | 2015-09-24 08:36:45 -0700 | [diff] [blame] | 448 | private String name = "State"; |
Ari Saha | 8983174 | 2015-06-26 10:31:48 -0700 | [diff] [blame] | 449 | |
Ray Milkey | 78e95a4 | 2015-09-24 08:36:45 -0700 | [diff] [blame] | 450 | public void start() throws StateMachineInvalidTransitionException { |
| 451 | log.warn("START transition from this state is not allowed."); |
| 452 | } |
Ari Saha | 8983174 | 2015-06-26 10:31:48 -0700 | [diff] [blame] | 453 | |
Ray Milkey | 78e95a4 | 2015-09-24 08:36:45 -0700 | [diff] [blame] | 454 | public void requestAccess() throws StateMachineInvalidTransitionException { |
| 455 | log.warn("REQUEST ACCESS transition from this state is not allowed."); |
| 456 | } |
| 457 | |
| 458 | public void radiusAccepted() throws StateMachineInvalidTransitionException { |
| 459 | log.warn("AUTHORIZE ACCESS transition from this state is not allowed."); |
| 460 | } |
| 461 | |
| 462 | public void radiusDenied() throws StateMachineInvalidTransitionException { |
| 463 | log.warn("DENY ACCESS transition from this state is not allowed."); |
| 464 | } |
| 465 | |
| 466 | public void logoff() throws StateMachineInvalidTransitionException { |
| 467 | log.warn("LOGOFF transition from this state is not allowed."); |
| 468 | } |
Ari Saha | 8983174 | 2015-06-26 10:31:48 -0700 | [diff] [blame] | 469 | } |
Thomas Vachuska | e989420 | 2015-07-30 11:59:07 -0700 | [diff] [blame] | 470 | |
Ray Milkey | 78e95a4 | 2015-09-24 08:36:45 -0700 | [diff] [blame] | 471 | /** |
| 472 | * Idle state: supplicant is logged of from the network. |
| 473 | */ |
| 474 | class Idle extends State { |
| 475 | private final Logger log = getLogger(getClass()); |
| 476 | private String name = "IDLE_STATE"; |
| 477 | |
| 478 | public void start() { |
| 479 | log.info("Moving from IDLE state to STARTED state."); |
| 480 | } |
Ari Saha | 8983174 | 2015-06-26 10:31:48 -0700 | [diff] [blame] | 481 | } |
Thomas Vachuska | e989420 | 2015-07-30 11:59:07 -0700 | [diff] [blame] | 482 | |
Ray Milkey | 78e95a4 | 2015-09-24 08:36:45 -0700 | [diff] [blame] | 483 | /** |
| 484 | * Started state: supplicant has entered the network and informed the authenticator. |
| 485 | */ |
| 486 | class Started extends State { |
| 487 | private final Logger log = getLogger(getClass()); |
| 488 | private String name = "STARTED_STATE"; |
| 489 | |
| 490 | public void requestAccess() { |
| 491 | log.info("Moving from STARTED state to PENDING state."); |
| 492 | } |
Ari Saha | 8983174 | 2015-06-26 10:31:48 -0700 | [diff] [blame] | 493 | } |
Thomas Vachuska | e989420 | 2015-07-30 11:59:07 -0700 | [diff] [blame] | 494 | |
Ray Milkey | 78e95a4 | 2015-09-24 08:36:45 -0700 | [diff] [blame] | 495 | /** |
| 496 | * Pending state: supplicant has been identified by the authenticator but has not access yet. |
| 497 | */ |
| 498 | class Pending extends State { |
| 499 | private final Logger log = getLogger(getClass()); |
| 500 | private String name = "PENDING_STATE"; |
| 501 | |
| 502 | public void radiusAccepted() { |
| 503 | log.info("Moving from PENDING state to AUTHORIZED state."); |
| 504 | } |
| 505 | |
| 506 | public void radiusDenied() { |
| 507 | log.info("Moving from PENDING state to UNAUTHORIZED state."); |
| 508 | } |
Ari Saha | 8983174 | 2015-06-26 10:31:48 -0700 | [diff] [blame] | 509 | } |
Thomas Vachuska | e989420 | 2015-07-30 11:59:07 -0700 | [diff] [blame] | 510 | |
Ray Milkey | 78e95a4 | 2015-09-24 08:36:45 -0700 | [diff] [blame] | 511 | /** |
| 512 | * Authorized state: supplicant port has been accepted, access is granted. |
| 513 | */ |
| 514 | class Authorized extends State { |
| 515 | private final Logger log = getLogger(getClass()); |
| 516 | private String name = "AUTHORIZED_STATE"; |
Ari Saha | 8983174 | 2015-06-26 10:31:48 -0700 | [diff] [blame] | 517 | |
Qianqian Hu | 0c34981 | 2016-02-15 17:25:22 +0800 | [diff] [blame] | 518 | public void start() { |
| 519 | log.info("Moving from AUTHORIZED state to STARTED state."); |
| 520 | } |
| 521 | |
Ray Milkey | 78e95a4 | 2015-09-24 08:36:45 -0700 | [diff] [blame] | 522 | public void logoff() { |
Ari Saha | 8983174 | 2015-06-26 10:31:48 -0700 | [diff] [blame] | 523 | |
Ray Milkey | 78e95a4 | 2015-09-24 08:36:45 -0700 | [diff] [blame] | 524 | log.info("Moving from AUTHORIZED state to IDLE state."); |
| 525 | } |
Ari Saha | 8983174 | 2015-06-26 10:31:48 -0700 | [diff] [blame] | 526 | } |
| 527 | |
Ray Milkey | 78e95a4 | 2015-09-24 08:36:45 -0700 | [diff] [blame] | 528 | /** |
| 529 | * Unauthorized state: supplicant port has been rejected, access is denied. |
| 530 | */ |
| 531 | class Unauthorized extends State { |
| 532 | private final Logger log = getLogger(getClass()); |
| 533 | private String name = "UNAUTHORIZED_STATE"; |
| 534 | |
ke han | 04e47f3 | 2016-10-28 14:15:43 +0800 | [diff] [blame] | 535 | public void start() { |
| 536 | log.info("Moving from UNAUTHORIZED state to STARTED state."); |
| 537 | } |
| 538 | |
Ray Milkey | 78e95a4 | 2015-09-24 08:36:45 -0700 | [diff] [blame] | 539 | public void logoff() { |
| 540 | log.info("Moving from UNAUTHORIZED state to IDLE state."); |
| 541 | } |
Ari Saha | 8983174 | 2015-06-26 10:31:48 -0700 | [diff] [blame] | 542 | } |
Ari Saha | 8983174 | 2015-06-26 10:31:48 -0700 | [diff] [blame] | 543 | |
| 544 | |
Ari Saha | 8983174 | 2015-06-26 10:31:48 -0700 | [diff] [blame] | 545 | } |