Java tutorial
/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package com.nkapps.billing.services; import java.math.BigInteger; import java.security.InvalidKeyException; import java.security.NoSuchAlgorithmException; import java.security.SignatureException; import java.security.cert.X509Certificate; import java.util.Date; import java.util.HashMap; import java.util.LinkedList; import java.util.List; import javax.servlet.http.HttpServletRequest; import org.bouncycastle.asn1.x500.AttributeTypeAndValue; import org.bouncycastle.asn1.x500.RDN; import org.bouncycastle.asn1.x500.X500Name; import org.bouncycastle.asn1.x500.style.BCStyle; import org.bouncycastle.util.encoders.Base64; import org.bouncycastle.util.encoders.Hex; import org.springframework.beans.BeanUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.MessageSource; import org.springframework.context.i18n.LocaleContextHolder; import org.springframework.core.env.Environment; import org.springframework.stereotype.Service; import com.nkapps.billing.models.Subject; /** * * @author nuraddin */ @Service("authService") public class AuthServiceImpl implements AuthService { @Autowired private AuthBean authBean; @Autowired private AuthenticationService authenticationService; @Autowired private Environment environment; @Autowired private MessageSource messageSource; @Override public void signOut(HttpServletRequest request) { authBean.setAuthenticated(false); } @Override public void signIn(HttpServletRequest request) throws Exception { String serialNumber = request.getParameter("serialNumber"); String signature = request.getParameter("signature"); if (serialNumber == null || serialNumber.trim().isEmpty()) { throw new Exception( messageSource.getMessage("auth.select_certificate", null, LocaleContextHolder.getLocale())); } if (signature == null || signature.trim().isEmpty()) { throw new Exception(messageSource.getMessage("auth.do_login", null, LocaleContextHolder.getLocale())); } AuthenticationService.Token token = authenticationService.getToken(serialNumber); if (token == null) { throw new Exception( messageSource.getMessage("auth.token_error_or_timout", null, LocaleContextHolder.getLocale())); } if (token.isTimedOut()) { throw new Exception( messageSource.getMessage("auth.token_timeout", null, LocaleContextHolder.getLocale())); } if (!request.getRemoteAddr().equals(token.getRemoteAddr())) { throw new Exception( messageSource.getMessage("auth.token_ip_mismatch", null, LocaleContextHolder.getLocale())); } /* Here code to check ds signature */ CertificateInfoLocal certInfoLocal = new CertificateInfoLocal(); authBean.setCertificateInfo(certInfoLocal); authBean.setAuthenticated(true); } @Override public CertificateInfoLocal getCertificateInfo() { return authBean.getCertificateInfo(); } @Override public boolean isAuthenticated() { return authBean.isAuthenticated(); } @Override public boolean isPrivileged() { CertificateInfoLocal issuerCertificateInfo = getCertificateInfo(); // check role return true; } public Subject extractSubject(CertificateInfoLocal certificateInfo) { String tin, name, organization, address, email, role; Short ns10Code, ns11Code, kind; HashMap<String, String> subjectInfos = new HashMap<>(); X500Name x500 = new X500Name(certificateInfo.getSubjectName()); for (RDN rdn : x500.getRDNs()) { AttributeTypeAndValue tv = rdn.getFirst(); String v = tv.getValue().toString().trim().replaceAll("\\s+", " "); if (!v.trim().isEmpty()) subjectInfos.put(BCStyle.INSTANCE.oidToDisplayName(tv.getType()), v); } role = certificateInfo.getRoleName(); Subject subject = new Subject(); // set subject datas subject.setRole(role); return subject; } public Short getKindOfTin(String tin) { if (tin.startsWith("2") || tin.startsWith("3")) { return 1; } else { return 2; } } @Override public Subject getSubject() { return extractSubject(getCertificateInfo()); } @Override public String getClientIp(HttpServletRequest request) { String ip = request.getRemoteAddr(); if (request.getHeader("X-Real-IP") != null && !"".equals(request.getHeader("X-Real-IP"))) { ip = request.getHeader("X-Real-IP"); } return ip; } @Override public boolean isAllowedByPropertyIp(String property, String clientIp) { boolean isAllowed = false; String ipStr = environment.getProperty(property); String[] ips = ipStr.split(","); for (String ip : ips) { if (ip.trim().equals(clientIp)) { isAllowed = true; break; } } return isAllowed; } }