/* * Copyright 2015 The Skfiy Open Association. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package org.skfiy.typhon.spi.auth.p; import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSONObject; import java.io.IOException; import java.nio.charset.StandardCharsets; import java.util.ArrayList; import java.util.List; import org.apache.http.HttpResponse; import org.apache.http.NameValuePair; import org.apache.http.client.ClientProtocolException; import org.apache.http.client.ResponseHandler; import org.apache.http.client.entity.UrlEncodedFormEntity; import org.apache.http.client.methods.HttpPost; import org.apache.http.impl.client.CloseableHttpClient; import org.apache.http.impl.client.HttpClientBuilder; import org.apache.http.message.BasicNameValuePair; import org.skfiy.typhon.packet.OAuth2; import org.skfiy.typhon.packet.Platform; import org.skfiy.typhon.spi.auth.OAuth2Exception; import org.skfiy.typhon.spi.auth.OAuthenticator; import org.skfiy.typhon.spi.auth.UserInfo; import org.skfiy.util.StreamUtils; /** * * @author Kevin Zou <kevinz@skfiy.org> */ public class Four399Authenticator implements OAuthenticator { private final HttpClientBuilder HC_BUILDER = HttpClientBuilder.create(); @Override public UserInfo authentic(OAuth2 oauth) { CloseableHttpClient hc = HC_BUILDER.build(); HttpPost httpPost = new HttpPost("http://dev.sj.4399api.net/Rest?ac=checkToken"); List<NameValuePair> nvps = new ArrayList<>(); nvps.add(new BasicNameValuePair("uid", oauth.getUid())); nvps.add(new BasicNameValuePair("access_token", oauth.getCode())); nvps.add(new BasicNameValuePair("app_key", "100979")); nvps.add(new BasicNameValuePair("app_secret", "da7be84d18a8aec8a5a45bbc6e5889e9")); try { httpPost.setEntity(new UrlEncodedFormEntity(nvps)); JSONObject json = hc.execute(httpPost, new ResponseHandler<JSONObject>() { @Override public JSONObject handleResponse(HttpResponse response) throws ClientProtocolException, IOException { String str = StreamUtils.copyToString(response.getEntity().getContent(), StandardCharsets.UTF_8); return JSON.parseObject(str); } }); if (json.getIntValue("code") != 10000) { throw new OAuth2Exception(json.getString("message")); } UserInfo info = new UserInfo(); info.setUsername(getPlatform().getLabel() + "-" + json.getJSONObject("data").getString("username")); info.setPlatform(getPlatform()); return info; } catch (IOException ex) { throw new OAuth2Exception("4399认证失败", ex); } finally { try { hc.close(); } catch (IOException ex) { } } } @Override public Platform getPlatform() { return Platform.four399; } }