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

io.github.uac.UAC Maven / Gradle / Ivy

There is a newer version: 2.0.3
Show newest version
package io.github.uac;

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

import com.eshore.tools.Bytes;
import com.eshore.tools.HTTP;
import com.eshore.tools.SimpleJson;
import com.eshore.tools.Tokens;
import com.eshore.tools.pbkdf2.Hash;
import com.eshore.tools.pbkdf2.Sha256;
import com.eshore.uas.server.impl.TOKEN64;

/**
 *访问uas资源的SDK
 * @author eshore
 *
 */
public class UAC {
	private String server;
	
	private String id;
	private String name;
	private String roles;
	private String hash;
	private String account;
	private String cellphone;
	private String mail;
	private String orgId;
	private Integer status;//-1 登陆失败 1 登陆成功 0待处理
	private String msg;
	
	
	private boolean logoned;
	
	private static Charset charset;
	static {
		try {
		charset=Charset.forName("utf-8");
		}catch(Exception e) {
		}
	}
	
	public UAC(String server) {
		this.server=server;
		useCaptcha();
	}
	String capHash="";
	private String s4() {
		return Tokens.randomString(8);
	}
	private String capToken() {
		String uid=s4();
		String h = Bytes.toHexString(new Sha256().sum((uid+capHash).getBytes()));
		return Tokens.serialization(uid,s4(),h);
	}
	public void useCaptcha() {
		String versionString = new HTTP().url(server+"?e=version").get().asString(charset);
		SimpleJson sj = new SimpleJson();
		sj.fromString(versionString);
		Map v =(Map) sj.getRoot();
		capHash=(String) v.get("hash");
	}
	
	public boolean login(String account,String password) {
		String code =capToken();
		account=TOKEN64.encode(account.getBytes());
		password=TOKEN64.encode(password.getBytes());
		//TOKEN64
		String data=Tokens.serialization(account,
				Tokens.randomString(3),
				password,
				Tokens.randomString(4),
				code,
				"."
				);
		String loginString = new HTTP().url(server+"?e=user&uas_login="+data)
		.get().asString(charset);
		SimpleJson sj = new SimpleJson();
		sj.fromString(loginString);
		Map user =(Map) sj.getRoot();
		if(user.get("id")==null) {
			status=-1;
			msg=(String) user.get("msg");
			return false;
		}
			
		id=(String) user.get("id");
		name=(String) user.get("name");
		hash=(String) user.get("hash");
		account=(String) user.get("account");
		cellphone=(String) user.get("cellphone");
		mail=(String) user.get("mail");
		orgId=(String) user.get("orgId");
		logoned=true;
		status=1;
		return logoned;
	}
	
	public String getTicket() {
		if(!logoned||hash==null)return null;
		Hash h1 =new Sha256();
		Hash h2 =h1.getHash();
		String uid=Tokens.randomString(16);
		StringBuilder sb = new StringBuilder();
		sb.append(id).append(uid).append("0");
		String s1= Bytes.toHexString(h1.sum(sb.toString().getBytes()));
		sb = new StringBuilder();
		sb.append(s1).append(hash);
		String s2=Bytes.toHexString(h2.sum(sb.toString().getBytes()));
		return Tokens.serialization(id,uid,"0",s2);
	}
	
	
	
	public String getServer() {
		return server;
	}

	public void setServer(String server) {
		this.server = server;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getRoles() {
		return roles;
	}

	public void setRoles(String roles) {
		this.roles = roles;
	}

	public String getCellphone() {
		return cellphone;
	}

	public void setCellphone(String cellphone) {
		this.cellphone = cellphone;
	}

	public String getMail() {
		return mail;
	}

	public void setMail(String mail) {
		this.mail = mail;
	}

	public String getOrgId() {
		return orgId;
	}

	public void setOrgId(String orgId) {
		this.orgId = orgId;
	}

	public static Charset getCharset() {
		return charset;
	}

	public static void setCharset(Charset charset) {
		UAC.charset = charset;
	}

	public String getId() {
		return id;
	}

	public String getAccount() {
		return account;
	}
	
	public Integer getStatus() {
		return status;
	}

	public String getMsg() {
		return msg;
	}

	public static void main(String[] agrs) {
		//UAC uac = new UAC("http://132.122.1.16:9180/uas/server");
		UAC uac = new UAC("http://127.0.0.1/uas/server");
		uac.useCaptcha();
		uac.login("admin", "Shmilyhe5169");
		System.out.println(uac.name);
		System.out.println(uac.getTicket());
		if(uac.getStatus()==-1) {
			System.err.println(uac.getMsg());
		}
		String html = new HTTP().url("http://127.0.0.1/test").header("Authorization","uas "+uac.getTicket())
		.get().asString();
	}
	
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy