BrightSide Workbench Full Report + Source Code
AssertionProvider.java
Go to the documentation of this file.
1 /*
2  * TurrĂ³ i Cutiller Foundation. License notice.
3  * Copyright (C) 2019 Lluis TurrĂ³ Cutiller <http://www.turro.org/>
4  *
5  * This program is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU Affero General Public License as published by
7  * the Free Software Foundation, either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU Affero General Public License for more details.
14  *
15  * You should have received a copy of the GNU Affero General Public License
16  * along with this program. If not, see <http://www.gnu.org/licenses/>.
17  */
18 
19 package org.turro.sso;
20 
21 import java.io.IOException;
22 import java.nio.charset.StandardCharsets;
23 import java.util.Date;
24 import java.util.logging.Level;
25 import java.util.logging.Logger;
26 import javax.servlet.http.Cookie;
27 import javax.servlet.http.HttpServletRequest;
28 import javax.servlet.http.HttpServletResponse;
29 import org.amic.util.date.CheckDate;
30 import org.apache.commons.codec.DecoderException;
31 import org.turro.elephant.context.ElephantContext;
32 import org.turro.elephant.db.ElephantPU;
33 import org.turro.elephant.db.WhereClause;
34 import org.turro.elephant.entities.db.SSOIdentity;
35 import org.turro.elephant.impl.util.CookieUtil;
36 import org.turro.elephant.sso.IAssertionProvider;
37 import org.turro.elephant.sso.ISSOIdentity;
38 import org.turro.jpa.Dao;
39 import org.turro.plugin.contacts.IContact;
40 import org.turro.sql.SqlClause;
41 
46 @ElephantAssertion
47 public class AssertionProvider implements IAssertionProvider {
48 
49  private static final int VALID_DAYS = 90;
50  private static final String ASSERTION_ID = "_elac";
51 
52  @Override
53  public boolean hasAssertion(HttpServletRequest request) {
54  return CookieUtil.getCookie(request, ASSERTION_ID) != null;
55  }
56 
57  @Override
58  public void killAssertion(HttpServletResponse response) {
59  CookieUtil.deleteCookie(response, ASSERTION_ID, "/");
60  }
61 
62  @Override
63  public ISSOIdentity getAssertion(HttpServletRequest request) {
64  Cookie assertion = CookieUtil.getCookie(request, ASSERTION_ID);
65  if(assertion != null) {
66  try {
67  Dao dao = new ElephantPU();
68  return dao.find(SSOIdentity.class, new String(CookieUtil.decryptCookieValue(assertion), StandardCharsets.UTF_8));
69  } catch (IOException | DecoderException ex) {
70  Logger.getLogger(AssertionProvider.class.getName()).log(Level.SEVERE, ElephantContext.logMsg(null), ex);
71  }
72  }
73  return null;
74  }
75 
76  @Override
77  public void setAssertion(byte[] assertion, byte[] clientId, IContact contact,
78  String details, String remoteIP, String geoIP, HttpServletResponse response) {
79  if(assertion != null) {
80  SSOIdentity identity = new SSOIdentity();
81  identity.setClientAssertion(assertion);
82  identity.setClientId(clientId);
83  identity.setContact(contact);
84  updateAssertion(details, remoteIP, geoIP, response, identity);
85  }
86  }
87 
88  @Override
89  public void updateAssertion(String details, String remoteIP, String geoIP, HttpServletResponse response, ISSOIdentity identity) {
90  if(identity != null) {
91  CookieUtil.encryptCookie(response, ASSERTION_ID, identity.getClientAssertion(), "/", VALID_DAYS*24*60*60);
92  ((SSOIdentity) identity).setDateUsed(new Date());
93  ((SSOIdentity) identity).setDetails(details);
94  ((SSOIdentity) identity).setRemoteIp(remoteIP);
95  ((SSOIdentity) identity).setGeoIp(geoIP);
96  new ElephantPU().saveObject(identity);
97  removeOldAssertions();
98  }
99  }
100 
101  @Override
102  public void removeAssertion(HttpServletRequest request, HttpServletResponse response, IContact contact) {
103  CookieUtil.deleteCookie(response, ASSERTION_ID, "/");
104  WhereClause wc = new WhereClause();
105  wc.addClause("delete from SSOIdentity");
106  wc.addClause("where idContact = :id");
107  wc.addNamedValue("id", contact.getId());
108  wc.addClause("and clientId = :client");
109  wc.addNamedValue("client", SSO.getSSO().getCurrentId(request));
110  new ElephantPU().executeUpdate(wc);
111  }
112 
113  @Override
114  public String getGeoIP(String remoteIP) {
115  return SqlClause.select("max(geoIp)").from("SSOIdentity")
116  .where().equal("remoteIp", remoteIP)
117  .dao(new ElephantPU()).singleResult(String.class);
118  }
119 
120  private void removeOldAssertions() {
121  WhereClause wc = new WhereClause();
122  wc.addClause("delete from SSOIdentity");
123  wc.addClause("where dateUsed < :date");
124  wc.addNamedValue("date", new CheckDate().addDays(-VALID_DAYS).getDate());
125  new ElephantPU().executeUpdate(wc);
126  }
127 
128 }
void addNamedValue(String name, Object value)
void setClientAssertion(byte[] clientAssertion)
static Cookie getCookie(HttpServletRequest request, String name)
Definition: CookieUtil.java:36
static byte[] decryptCookieValue(Cookie cookie)
Definition: CookieUtil.java:69
static void encryptCookie(HttpServletResponse response, String name, byte[] value, String path, int age)
Definition: CookieUtil.java:60
static void deleteCookie(HttpServletResponse response, String name, String path)
Definition: CookieUtil.java:53
int executeUpdate(String query)
Definition: Dao.java:463
void updateAssertion(String details, String remoteIP, String geoIP, HttpServletResponse response, ISSOIdentity identity)
boolean hasAssertion(HttpServletRequest request)
ISSOIdentity getAssertion(HttpServletRequest request)
void killAssertion(HttpServletResponse response)
String getGeoIP(String remoteIP)
void removeAssertion(HttpServletRequest request, HttpServletResponse response, IContact contact)
void setAssertion(byte[] assertion, byte[] clientId, IContact contact, String details, String remoteIP, String geoIP, HttpServletResponse response)
static IElephantSSO getSSO()
Definition: SSO.java:49
byte[] getCurrentId(HttpServletRequest request)