|
|
@ -39,11 +39,16 @@ public class Backend extends PathHandler { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
private boolean authorize(HttpExchange ex, Session session) throws IOException { |
|
|
|
private boolean authorize(HttpExchange ex, Session session) throws IOException { |
|
|
|
var json = json(ex); |
|
|
|
var json = json(ex); |
|
|
|
var clientId = json.getString(CLIENT_ID); |
|
|
|
var clientId = json.getString(CLIENT_ID); |
|
|
|
|
|
|
|
var optClient = clients.getClient(clientId); |
|
|
|
|
|
|
|
if (optClient.isEmpty()) return sendEmptyResponse(HTTP_NOT_FOUND, ex); |
|
|
|
|
|
|
|
var client = optClient.get(); |
|
|
|
var redirect = json.getString(REDIRECT_URI); |
|
|
|
var redirect = json.getString(REDIRECT_URI); |
|
|
|
System.out.println(json); |
|
|
|
if (!client.redirectUris().contains(redirect)) return sendEmptyResponse(HTTP_BAD_REQUEST, ex); |
|
|
|
return sendEmptyResponse(HTTP_NOT_FOUND, ex); |
|
|
|
var state = json.getString(STATE); |
|
|
|
|
|
|
|
var code = client.generateCode(); |
|
|
|
|
|
|
|
return sendContent(ex, Map.of(CODE,code,REDIRECT_URI,redirect,STATE,state)); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
private boolean clients(HttpExchange ex, Session session) throws IOException { |
|
|
|
private boolean clients(HttpExchange ex, Session session) throws IOException { |
|
|
@ -117,6 +122,8 @@ public class Backend extends PathHandler { |
|
|
|
switch (path) { |
|
|
|
switch (path) { |
|
|
|
case "/login": |
|
|
|
case "/login": |
|
|
|
return doLogin(ex); |
|
|
|
return doLogin(ex); |
|
|
|
|
|
|
|
case "/token": |
|
|
|
|
|
|
|
return provideToken(ex); |
|
|
|
} |
|
|
|
} |
|
|
|
var optSession = getSession(ex); |
|
|
|
var optSession = getSession(ex); |
|
|
|
if (optSession.isEmpty()) return sendEmptyResponse(HTTP_UNAUTHORIZED, ex); |
|
|
|
if (optSession.isEmpty()) return sendEmptyResponse(HTTP_UNAUTHORIZED, ex); |
|
|
@ -164,17 +171,23 @@ public class Backend extends PathHandler { |
|
|
|
return sendEmptyResponse(HTTP_OK, ex); |
|
|
|
return sendEmptyResponse(HTTP_OK, ex); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
private boolean openidConfig(HttpExchange ex) throws IOException { |
|
|
|
private boolean provideToken(HttpExchange ex) throws IOException { |
|
|
|
JSONObject json = new JSONObject(); |
|
|
|
System.err.printf("%s.provideToken(ex) not implemented!\n",getClass().getSimpleName()); |
|
|
|
|
|
|
|
var json = json(ex); |
|
|
|
json.put("authorization_endpoint", hostname(ex) + "/web/authorization.html"); |
|
|
|
System.err.println(json); |
|
|
|
return sendContent(ex, json); |
|
|
|
return sendEmptyResponse(HTTP_NOT_FOUND,ex); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private boolean openidConfig(HttpExchange ex) throws IOException { |
|
|
|
|
|
|
|
return sendContent(ex, Map.of( |
|
|
|
|
|
|
|
"token_endpoint",hostname(ex)+"/api/token", |
|
|
|
|
|
|
|
"authorization_endpoint", hostname(ex) + "/web/authorization.html") |
|
|
|
|
|
|
|
); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
private boolean sendUserAndCookie(HttpExchange ex, Session session) throws IOException { |
|
|
|
private boolean sendUserAndCookie(HttpExchange ex, Session session) throws IOException { |
|
|
|
new SessionToken(session.id()).addTo(ex); |
|
|
|
new SessionToken(session.id()).addTo(ex); |
|
|
|
return sendContent(ex, new JSONObject(session.user().map(false))); |
|
|
|
return sendContent(ex, session.user().map(false)); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
private boolean updatePassword(HttpExchange ex, Session session) throws IOException { |
|
|
|
private boolean updatePassword(HttpExchange ex, Session session) throws IOException { |
|
|
@ -193,7 +206,7 @@ public class Backend extends PathHandler { |
|
|
|
return sendError(ex, "password mismatch"); |
|
|
|
return sendError(ex, "password mismatch"); |
|
|
|
} |
|
|
|
} |
|
|
|
users.updatePassword(user, newPass1); |
|
|
|
users.updatePassword(user, newPass1); |
|
|
|
return sendContent(ex, new JSONObject(user.map(false))); |
|
|
|
return sendContent(ex, user.map(false)); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
private boolean updateUser(HttpExchange ex, Session session) throws IOException { |
|
|
|
private boolean updateUser(HttpExchange ex, Session session) throws IOException { |
|
|
@ -206,7 +219,6 @@ public class Backend extends PathHandler { |
|
|
|
user.username(json.getString(USERNAME)); |
|
|
|
user.username(json.getString(USERNAME)); |
|
|
|
user.email(json.getString(EMAIL)); |
|
|
|
user.email(json.getString(EMAIL)); |
|
|
|
users.save(user); |
|
|
|
users.save(user); |
|
|
|
JSONObject response = new JSONObject(user.map(false)); |
|
|
|
return sendContent(ex, user.map(false)); |
|
|
|
return sendContent(ex, response); |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|