/
DefaultBootstrapSessionManager.java
101 lines (88 loc) · 3.8 KB
/
DefaultBootstrapSessionManager.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
/*******************************************************************************
* Copyright (c) 2016 Sierra Wireless and others.
*
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* and Eclipse Distribution License v1.0 which accompany this distribution.
*
* The Eclipse Public License is available at
* http://www.eclipse.org/legal/epl-v10.html
* and the Eclipse Distribution License is available at
* http://www.eclipse.org/org/documents/edl-v10.html.
*
* Contributors:
* Sierra Wireless - initial API and implementation
*******************************************************************************/
package org.eclipse.leshan.server.impl;
import java.util.List;
import org.eclipse.leshan.core.request.BootstrapFinishRequest;
import org.eclipse.leshan.core.request.Identity;
import org.eclipse.leshan.core.request.LwM2mRequest;
import org.eclipse.leshan.core.response.LwM2mResponse;
import org.eclipse.leshan.server.bootstrap.BootstrapFailureCause;
import org.eclipse.leshan.server.bootstrap.BootstrapSession;
import org.eclipse.leshan.server.bootstrap.BootstrapSessionManager;
import org.eclipse.leshan.server.security.BootstrapSecurityStore;
import org.eclipse.leshan.server.security.SecurityChecker;
import org.eclipse.leshan.server.security.SecurityInfo;
/**
* Implementation of a session manager.
* <p>
* Starting a session only checks credentials from BootstrapSecurityStore.
* <p>
* Nothing specific is done on session's end.
*/
public class DefaultBootstrapSessionManager implements BootstrapSessionManager {
private BootstrapSecurityStore bsSecurityStore;
private SecurityChecker securityChecker;
/**
* Create a {@link DefaultBootstrapSessionManager} using a default {@link SecurityChecker} to accept or refuse new
* {@link BootstrapSession}.
*
* @param bsSecurityStore the {@link BootstrapSecurityStore} used by default {@link SecurityChecker}.
*/
public DefaultBootstrapSessionManager(BootstrapSecurityStore bsSecurityStore) {
this(bsSecurityStore, new SecurityChecker());
}
/**
* Create a {@link DefaultBootstrapSessionManager}.
*
* @param bsSecurityStore the {@link BootstrapSecurityStore} used by {@link SecurityChecker}.
* @param securityChecker used to accept or refuse new {@link BootstrapSession}.
*/
public DefaultBootstrapSessionManager(BootstrapSecurityStore bsSecurityStore, SecurityChecker securityChecker) {
this.bsSecurityStore = bsSecurityStore;
this.securityChecker = securityChecker;
}
@Override
public BootstrapSession begin(String endpoint, Identity clientIdentity) {
boolean authorized;
if (bsSecurityStore != null) {
List<SecurityInfo> securityInfos = bsSecurityStore.getAllByEndpoint(endpoint);
authorized = securityChecker.checkSecurityInfos(endpoint, clientIdentity, securityInfos);
} else {
authorized = true;
}
return new DefaultBootstrapSession(endpoint, clientIdentity, authorized);
}
@Override
public void end(BootstrapSession bsSession) {
}
@Override
public void failed(BootstrapSession bsSession, BootstrapFailureCause cause) {
}
@Override
public void onResponseSuccess(LwM2mRequest<? extends LwM2mResponse> request) {
}
@Override
public BootstrapPolicy onResponseError(LwM2mRequest<? extends LwM2mResponse> request, LwM2mResponse response) {
if (request instanceof BootstrapFinishRequest) {
return BootstrapPolicy.STOP;
}
return BootstrapPolicy.CONTINUE;
}
@Override
public BootstrapPolicy onRequestFailure(LwM2mRequest<? extends LwM2mResponse> request, Throwable cause) {
return BootstrapPolicy.STOP;
}
}