Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package SagaBG.SeP.CRIS.PortalView.backing.user.login;
- import SagaBG.SeP.CRIS.DomainObject.AuthorizationData;
- import SagaBG.SeP.CRIS.DomainObject.InsuranceTaxPayer;
- import SagaBG.SeP.CRIS.Exceptions.CustomException;
- import SagaBG.SeP.CRIS.PortalView.common.utils.ELUtils;
- import SagaBG.SeP.CRIS.WebFacadeLayer.WebFacade;
- import java.io.IOException;
- import java.util.AbstractMap;
- import java.util.List;
- import java.util.Set;
- import javax.faces.application.FacesMessage;
- import javax.faces.context.ExternalContext;
- import javax.faces.context.FacesContext;
- import javax.faces.event.ActionEvent;
- import javax.faces.validator.ValidatorException;
- import javax.naming.NamingException;
- import javax.security.auth.Subject;
- import javax.security.auth.callback.CallbackHandler;
- import javax.security.auth.login.FailedLoginException;
- import javax.security.auth.login.LoginException;
- import javax.servlet.RequestDispatcher;
- import javax.servlet.ServletException;
- import javax.servlet.http.HttpServletRequest;
- import javax.servlet.http.HttpServletResponse;
- import javax.servlet.http.HttpSession;
- import oracle.adf.share.ADFContext;
- import oracle.adf.view.rich.render.ClientEvent;
- import weblogic.security.URLCallbackHandler;
- import weblogic.security.services.Authentication;
- import weblogic.servlet.security.ServletAuthentication;
- public class LoginTaxPayer {
- private static final String AuthorizationDataUserType = "OBVEZNIK";
- public LoginTaxPayer() {
- super();
- }
- private String _username;
- private String _password;
- private String _personalIdentityNumber;
- private String _pib;
- private String _taxPayerIdentificator;
- private String _taxPayerCRNumber;
- private String _taxPayerName;
- String errorText;
- public void setUsername(String _username) {
- this._username = _username;
- }
- public String getUsername() {
- return _username;
- }
- public void setPassword(String _password) {
- this._password = _password;
- }
- public String getPassword() {
- return _password;
- }
- public void setPersonalIdentityNumber(String _personalIdentityNumber) {
- this._personalIdentityNumber = _personalIdentityNumber;
- }
- public String getPersonalIdentityNumber() {
- return _personalIdentityNumber;
- }
- public void setPib(String _pib) {
- this._pib = _pib;
- }
- public String getPib() {
- return _pib;
- }
- public String doLogin() {
- String un = _username;
- byte[] pw = (_password != null) ? _password.getBytes() : new byte[0];
- FacesContext ctx = FacesContext.getCurrentInstance();
- HttpServletRequest request =
- (HttpServletRequest)ctx.getExternalContext().getRequest();
- CallbackHandler handler = new URLCallbackHandler(un, pw);
- try {
- Subject mySubject = Authentication.login(handler);
- ServletAuthentication.runAs(mySubject, request);
- ServletAuthentication.generateNewSessionID(request);
- String viewId = ctx.getViewRoot().getViewId();
- if (viewId.equals("/Login.jspx")) {
- viewId = "/home.jspx";
- }
- String loginUrl = "/adfAuthentication?success_url=/faces" + viewId;
- HttpServletResponse response =
- (HttpServletResponse)ctx.getExternalContext().getResponse();
- // ADFContext.getCurrent().getSessionScope().put("personalIdentityNumber", _personalIdentityNumber);
- // ADFContext.getCurrent().getSessionScope().put("pib", _pib);
- sendForward(request, response, loginUrl);
- } catch (FailedLoginException fle) {
- FacesMessage msg =
- new FacesMessage(FacesMessage.SEVERITY_ERROR, "Neuspešna prijava",
- fle.getMessage());
- ctx.addMessage(null, msg);
- } catch (LoginException le) {
- reportUnexpectedLoginError("LoginException", le);
- }
- return null;
- }
- public String doCertificateLogin(ClientEvent event) {
- String ticket = (String)event.getParameters().get("ticket");
- return doCertificateLogin(ticket, true);
- }
- public String doCertificateLogin(String ticket,
- String taxPayerIdentificator) {
- _taxPayerIdentificator = taxPayerIdentificator;
- return doCertificateLogin(ticket, false);
- }
- private String doCertificateLogin(String ticket, boolean redirect) {
- if ((Boolean)ELUtils.evaluateEL("#{loginBean.taxPayerLoginAllowed}") ==
- false) {
- throw new RuntimeException("Tax payer cannot be logged on this instance");
- }
- if (ticket == null || ticket.length() == 0) {
- reportUnexpectedLoginError("LoginException",
- new RuntimeException("Ticket cannot be null"));
- return null;
- }
- String un = _username;
- byte[] pw = (_password != null) ? _password.getBytes() : new byte[0];
- FacesContext ctx = FacesContext.getCurrentInstance();
- HttpServletRequest request =
- (HttpServletRequest)ctx.getExternalContext().getRequest();
- TicketCallbackHandler handler =
- new TicketCallbackHandler(ticket, _taxPayerIdentificator,
- "Obveznik");
- try {
- Subject mySubject = Authentication.login(handler);
- if (handler.getError() != null) {
- String error = handler.getError();
- if (error.equalsIgnoreCase("NIJE_AKTIVAN"))
- errorText = "Korisnik nije aktivan u sistemu CROSO";
- if (error.equalsIgnoreCase("ISTEKAO_SERTIFIKAT"))
- errorText = "Sertifikat Vam je istekao.";
- throw new ValidatorException(new FacesMessage(errorText));
- }
- _personalIdentityNumber =
- getPersonalIdentityNumber(mySubject.getPublicCredentials());
- loadTaxPayerCRNumber();
- setUserData(mySubject.getPublicCredentials());
- ServletAuthentication.runAs(mySubject, request);
- ServletAuthentication.generateNewSessionID(request);
- if (redirect) {
- String viewId = ctx.getViewRoot().getViewId();
- if (viewId.equals("/Login.jspx")) {
- viewId = "/home.jspx";
- }
- String loginUrl =
- "/adfAuthentication?success_url=/faces" + viewId;
- HttpServletResponse response =
- (HttpServletResponse)ctx.getExternalContext().getResponse();
- sendForward(request, response, loginUrl);
- }
- } catch (FailedLoginException fle) {
- String greska = "";
- if ("Signed XML document not valid, cannot verify signature.".equals(fle.getMessage()))
- greska = "Poruka je nevalidna, provera potpisa nije uspela.";
- if ("Signed XML document not valid, wrong challenge number.".equals(fle.getMessage()))
- greska =
- "Poruka je nevalidna, serverski broj nije dobar, pokušajte ponovo.";
- if ("igned XML document not valid, certificate is revoked.".equals(fle.getMessage()))
- greska =
- "Poruka je nevalidna, vaš sertifikat je povu?en, kontaktirajte sertifikaciono telo za više detalja.";
- if ("Signed XML document not valid, certificate chain is incomplete.".equals(fle.getMessage()))
- greska =
- "Poruka je nevalidna, sertifikat nije izdalo verifikovano sertifikaciono telo.";
- if ("Signed XML document not valid, certificate has expired.".equals(fle.getMessage()))
- greska = "Poruka je nevalidna, vaš sertifikat je istekao.";
- if ("Social identity number cannot be located".equals(fle.getMessage()))
- greska = "Poruka je nevalidna, nije prona?en JMBG.";
- if (greska.equals(""))
- greska = fle.getMessage();
- FacesMessage msg =
- new FacesMessage(FacesMessage.SEVERITY_ERROR, "Neuspešna prijava",
- fle.getMessage());
- ctx.addMessage(null, msg);
- } catch (ValidatorException ve) {
- reportUnexpectedLoginError(errorText,ve);
- } catch (LoginException le) {
- String error = le.getMessage();
- reportUnexpectedLoginError("LoginException", le);
- } catch (CustomException e) {
- FacesMessage msg =
- new FacesMessage(FacesMessage.SEVERITY_ERROR, "Neuspešna prijava",
- e.getMessage());
- ctx.addMessage(null, msg);
- }
- return null;
- }
- private void sendForward(HttpServletRequest request,
- HttpServletResponse response, String forwardUrl) {
- FacesContext ctx = FacesContext.getCurrentInstance();
- RequestDispatcher dispatcher =
- request.getRequestDispatcher(forwardUrl);
- try {
- dispatcher.forward(request, response);
- } catch (ServletException se) {
- reportUnexpectedLoginError("ServletException", se);
- } catch (IOException ie) {
- reportUnexpectedLoginError("IOException", ie);
- }
- ctx.responseComplete();
- }
- private void reportUnexpectedLoginError(String errType, Exception e) {
- FacesMessage msg =
- new FacesMessage(FacesMessage.SEVERITY_ERROR, "Neocekivana gre\u0161ka prilikom logovanja",
- "Neo\u010Dekivana gre\u0161ka prilikom logovanja (" +
- errType +
- "), molim Vas kontaktirajte korisni\u010Dki servis.");
- FacesContext.getCurrentInstance().addMessage(null, msg);
- e.printStackTrace();
- }
- private void setUserData(Set<Object> publicCredentials) {
- ADFContext.getCurrent().getSessionScope().put("authorizationDataUserType",
- AuthorizationDataUserType);
- ADFContext.getCurrent().getSessionScope().put("taxPayerId",
- _taxPayerIdentificator);
- // ako identifikator ima 9 cifara onda je pib, ako ima 13 cifara onda je jmbg
- if (_taxPayerIdentificator != null) {
- if (isPib(_taxPayerIdentificator)) {
- ADFContext.getCurrent().getSessionScope().put("pib",
- _taxPayerIdentificator);
- FacesContext fctx = FacesContext.getCurrentInstance();
- ExternalContext ectx = fctx.getExternalContext();
- HttpSession userSession = (HttpSession) ectx.getSession(false);
- userSession.setAttribute("sesija.osiguranik.pib",_taxPayerIdentificator);
- } else if (isPersonalIdentityNumber(_taxPayerIdentificator)) {
- ADFContext.getCurrent().getSessionScope().put("taxPayerPersonalIdentityNumber",
- _taxPayerIdentificator);
- }
- }
- ADFContext.getCurrent().getSessionScope().put("taxPayerCRNumber",
- _taxPayerCRNumber);
- ADFContext.getCurrent().getSessionScope().put("taxPayerName",
- _taxPayerName);
- for (Object publicCredential : publicCredentials) {
- if (publicCredential instanceof AbstractMap.SimpleEntry) {
- AbstractMap.SimpleEntry<Object, Object> entryCredential =
- (AbstractMap.SimpleEntry<Object, Object>)publicCredential;
- Object key = entryCredential.getKey();
- if ("JMBG".equals(key)) {
- ADFContext.getCurrent().getSessionScope().put("personalIdentityNumber",
- entryCredential.getValue());
- }
- }
- }
- }
- private String getPersonalIdentityNumber(Set<Object> publicCredentials) {
- for (Object publicCredential : publicCredentials) {
- if (publicCredential instanceof AbstractMap.SimpleEntry) {
- AbstractMap.SimpleEntry<Object, Object> entryCredential =
- (AbstractMap.SimpleEntry<Object, Object>)publicCredential;
- Object key = entryCredential.getKey();
- if ("JMBG".equals(key)) {
- return (String)entryCredential.getValue();
- }
- }
- }
- throw new RuntimeException("JMBG not found in publicCredentials");
- }
- public void setTaxPayerIdentificator(String _taxPayerIdentificator) {
- this._taxPayerIdentificator = _taxPayerIdentificator;
- }
- public String getTaxPayerIdentificator() {
- return _taxPayerIdentificator;
- }
- public void doCertificateLogin(ActionEvent actionEvent) throws CustomException {
- String ticket = (String)ELUtils.evaluateEL("#{pageFlowScope.ticket}");
- _taxPayerIdentificator =
- (String)ELUtils.evaluateEL("#{pageFlowScope.taxPayerIdentificator}");
- // loadTaxPayerCRNumber();
- doCertificateLogin(ticket, true);
- }
- private void loadTaxPayerCRNumber() throws CustomException {
- AuthorizationData authorizationData = new AuthorizationData();
- // TODO: prebaci ovo u konfiguraciju
- authorizationData.setUserType("APLIKACIJA");
- authorizationData.setUserId("CLWEBC.CRIS");
- WebFacade facade = new WebFacade();
- InsuranceTaxPayer taxPayer;
- // TODO: ovo ce mozda morati da se uradi nakon login-a, nakon sto se ucita authorization data
- if (isPib(_taxPayerIdentificator))
- taxPayer =
- facade.GetTaxPayerDetailsByPib(_taxPayerIdentificator, authorizationData);
- else if (isPersonalIdentityNumber(_taxPayerIdentificator))
- taxPayer =
- facade.GetTaxPayerDetailsByPersonalIdentityNumber(_taxPayerIdentificator,
- authorizationData);
- else
- throw new RuntimeException(String.format("Unexpected value of taxPayerIdentificator '%s'",
- _taxPayerIdentificator));
- if (taxPayer == null)
- HandleError("Ne postoji registrovani korisnik sa datim sertifikatom, kod pravnog lica sa PIB: " +
- _taxPayerIdentificator);
- //throw new RuntimeException(String.format("No tax payer data returned from service for identificator '%s'", _taxPayerIdentificator));
- _taxPayerCRNumber = taxPayer.getCrNumber();
- _taxPayerName = taxPayer.getNameOfInsuranceTaxPayer();
- }
- private void HandleError(String message) {
- FacesMessage msg =
- new FacesMessage(FacesMessage.SEVERITY_ERROR, "Greška", message);
- FacesContext.getCurrentInstance().addMessage(null, msg);
- }
- public boolean isPib(String taxPayerIdentificator) {
- return taxPayerIdentificator != null &&
- taxPayerIdentificator.length() == 9;
- }
- public boolean isPersonalIdentityNumber(String taxPayerIdentificator) {
- return taxPayerIdentificator != null &&
- taxPayerIdentificator.length() == 13;
- }
- public void setErrorText(String errorText) {
- this.errorText = errorText;
- }
- public String getErrorText() {
- return errorText;
- }
- }
Add Comment
Please, Sign In to add comment