All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.eshore.uas.auth.DefaultAuth Maven / Gradle / Ivy

There is a newer version: 2.0.3
Show newest version
package com.eshore.uas.auth;

import java.nio.charset.Charset;
import java.util.UUID;

import com.eshore.khala.utils.LRUCache;
import com.eshore.tools.B64;
import com.eshore.tools.Bytes;
import com.eshore.tools.Time;
import com.eshore.tools.Tokens;
import com.eshore.tools.pbkdf2.Hash;
import com.eshore.tools.pbkdf2.MD5;
import com.eshore.tools.pbkdf2.Sha256;

/**
 * Oauth 主认实现
 * @author eshore
 *
 */
public class DefaultAuth implements IAuth {
	//LRUCache cache = new LRUCache();
	static int duration=3600*24;
	static Charset charset =Charset.forName("utf-8");
	@Override
	public boolean auth(String app_id, String app_secret, String sign, String timestamp,String key) {
		String signC=md5(key+timestamp);
		if(!signC.equals(sign)) {
			return false;
		}
		int time = Time.getUnixTimestamp();
		int intime = Integer.parseInt(timestamp);
		if(Math.abs(time-intime)>duration) {
			return false;
		}
		String app_secretC=md5(key+sign);
		return app_secretC.equals(app_secret);
	}
	Hash hash = new MD5();
	private String md5(String str) {
		return B64.encode(hash.getHash().sum(str.getBytes(charset)));
	}
	
	Hash hasher = new Sha256();
	@Override
	public String hash(String key) {
		return  Bytes.toHexString(hasher.getHash().sum(key.getBytes(charset)));
	}
	
	@Override
	public boolean checkToken(String hash,String key) {
		try {
		String [] ks=Tokens.deserialization(hash);
		if(ks.length<4) {return false;}
		String appid=ks[0];
		String sgin=ks[1];
		String time=ks[2];
		String ramdom=ks[3];
		int now=Time.getUnixTimestamp();
		int create =Integer.parseInt(time);
		int dev=now-create;
		if(dev<-3600||dev>14*3600) {
			return false;
		}
		Hash h = hasher.getHash();
		h.write(appid.getBytes());
		h.write(time.getBytes());
		h.write(ramdom.getBytes());
		String sgin2=Bytes.toHexString(h.sum(key.getBytes()));
		return sgin2.equals(sgin);
		}catch(Exception e) {
			
		}
		
		return  false;
	}
	@Override
	public String genToken(String hash, String app_id) {
		//cache.put(app_id, hash, 3600000);
		String ramdom=UUID.randomUUID().toString().replaceAll("-", "");
		String time=String.valueOf(Time.getUnixTimestamp());
		Hash h = hasher.getHash();
		h.write(app_id.getBytes());
		h.write(time.getBytes());
		h.write(ramdom.getBytes());
		String sgin=Bytes.toHexString(h.sum(hash.getBytes()));
		return Tokens.serialization(app_id,sgin,time,ramdom);
	}
	
	
	public static void main(String srt[]) {
		DefaultAuth au = new DefaultAuth();
		String tk =au.genToken("qweqwe","123123");
		System.out.println(tk);
		System.out.println(au.checkToken(tk,"qweqwe"));
	}

	@Override
	public String getAppId(String token) {
		try {
			String [] ks=Tokens.deserialization(token);
			if(ks.length<4) {
				return null;
			}
			return ks[0];
		}catch(Exception e) {
			
		}
		return null;
	}
	

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy