forked from hap-java/HAP-Java
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathPairingManager.java
65 lines (56 loc) · 2.42 KB
/
PairingManager.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
package io.github.hapjava.server.impl.pairing;
import io.github.hapjava.server.HomekitAuthInfo;
import io.github.hapjava.server.impl.HomekitRegistry;
import io.github.hapjava.server.impl.http.HttpRequest;
import io.github.hapjava.server.impl.http.HttpResponse;
import io.github.hapjava.server.impl.responses.NotFoundResponse;
import io.github.hapjava.server.impl.responses.UnauthorizedResponse;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class PairingManager {
private static final Logger logger = LoggerFactory.getLogger(PairingManager.class);
private final HomekitAuthInfo authInfo;
private final HomekitRegistry registry;
private SrpHandler srpHandler;
public PairingManager(HomekitAuthInfo authInfo, HomekitRegistry registry) {
this.authInfo = authInfo;
this.registry = registry;
}
public HttpResponse handle(HttpRequest httpRequest) throws Exception {
PairSetupRequest req = PairSetupRequest.of(httpRequest.getBody());
if (req.getStage() == Stage.ONE) {
logger.trace("Starting pair for " + registry.getLabel());
srpHandler = new SrpHandler(authInfo.getPin(), authInfo.getSalt());
return srpHandler.step1();
} else if (req.getStage() == Stage.TWO) {
logger.trace("Entering second stage of pair for " + registry.getLabel());
if (srpHandler == null) {
logger.warn("Received unexpected stage 2 request for " + registry.getLabel());
return new UnauthorizedResponse();
} else {
try {
return srpHandler.step2((PairSetupRequest.Stage2Request) req);
} catch (Exception e) {
srpHandler = null; // You don't get to try again - need a new key
logger.warn("Exception encountered while processing pairing request", e);
return new UnauthorizedResponse();
}
}
} else if (req.getStage() == Stage.THREE) {
logger.trace("Entering third stage of pair for " + registry.getLabel());
if (srpHandler == null) {
logger.warn("Received unexpected stage 3 request for " + registry.getLabel());
return new UnauthorizedResponse();
} else {
FinalPairHandler handler = new FinalPairHandler(srpHandler.getK(), authInfo);
try {
return handler.handle(req);
} catch (Exception e) {
logger.warn("Exception while finalizing pairing", e);
return new UnauthorizedResponse();
}
}
}
return new NotFoundResponse();
}
}