package net.davidvoid.thor.lightning.service.security;

import io.jsonwebtoken.Claims;
import io.jsonwebtoken.Jws;
import io.jsonwebtoken.JwtException;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

import net.davidvoid.thor.lightning.exception.AuthenticationException;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.stereotype.Component;

/**
 * Created by david on 4/26/16.
 */
@Component
public class JwtAuthenticationService {
    static Log logger = LogFactory.getLog(JwtAuthenticationService.class);

    String secretKey = null;
    
    public JwtAuthenticationService() {
        InputStream in = this.getClass().getClassLoader().getResourceAsStream("config/key.hmac");
        BufferedReader reader = new BufferedReader(new InputStreamReader(in));

        try {
            secretKey = reader.readLine();
        } catch (IOException e) {
            logger.fatal("unable to read HMAC key");
        } finally {
            try {
                reader.close();
            } catch (IOException e) {
                logger.fatal("failed to close file");
            }
        }
    }

    public Authentication authenticate(String jwtToken) {
        try {
            Jws<Claims> claims = Jwts.parser().setSigningKey(secretKey).parseClaimsJws(jwtToken);
            String username = claims.getBody().getSubject();
            if (username == null) throw new AuthenticationException("no subject present");

            UsernamePasswordAuthenticationToken token = new UsernamePasswordAuthenticationToken(username, "");
            return token;

        } catch (JwtException e) {
            throw new AuthenticationException("invalid JWT token", e);
        }
    }
    
    public String getToken(String username) {
        return Jwts.builder().setSubject(username).signWith(SignatureAlgorithm.HS512, secretKey).compact();
    }
}

@@ -338,16 +338,67 @@
 mReader;
+%0Aimport java.util.ArrayList;%0Aimport java.util.List;
 %0A%0Aimport
@@ -689,24 +689,158 @@
 entication;%0A
+import org.springframework.security.core.GrantedAuthority;%0Aimport org.springframework.security.core.authority.SimpleGrantedAuthority;%0A
 import org.s
@@ -1952,24 +1952,145 @@
 present%22);%0A%0A
+            List%3CGrantedAuthority%3E roles = new ArrayList%3C%3E();%0A            roles.add(new SimpleGrantedAuthority(%22USER%22));%0A
             
@@ -2185,16 +2185,23 @@
 name, %22%22
+, roles
 );%0A     

return Authentiction with roles