Fakultas Ilmu Komputer UI

Commit d67d46ef authored by Hafiyyan's avatar Hafiyyan
Browse files

Merge branch 'ci-branch' into 'master'

Merging Oauth to Master

See merge request !1
parents 62ed5a09 9f5cbc59
Pipeline #1361 canceled with stages
image: hafiyyan94/androidsdk25gitlabci:newSet
before_script:
- chmod +x ./gradlew
image: hafiyyan94/androidsdk25gitlabci:newTry
stages:
- build
- test
unitTests:
stage: test
before_script:
- export GRADLE_USER_HOME=`pwd`/.gradle
- mkdir -p $GRADLE_USER_HOME
- chmod +x ./gradlew
cache:
paths:
- .gradle/wrapper
- .gradle/caches
build:
stage: build
script:
- ./gradlew testDebugUnitTestCoverage
- ./gradlew assembleDebug
- ./gradlew assembleDebugAndroidTest
artifacts:
paths:
- app/build/reports/jacoco/
- app/build/outputs/
expire_in: 1 week
test:
stage: test
script:
- echo "no" | $ANDROID_HOME/tools/bin/avdmanager create avd -n test2 -k "system-images;android-24;default;armeabi-v7a"
- echo "no" | $ANDROID_HOME/emulator/emulator -avd test2 -wipe-data -noaudio -no-window -gpu off -verbose -no-accel && /helpers/wait-for-avd-boot.sh
\ No newline at end of file
......@@ -42,10 +42,4 @@ public class LoginInstrumentedTest {
// Type text and then press the button.
onView(withId(R.id.login)).check(matches(withText(mButtonTextExpected)));
}
@Test
public void clickLogin_showHomeActivity(){
onView(withId(R.id.login)).perform(click());
onView(withId(R.id.bottomBar)).check(matches(isDisplayed()));
}
}
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="id.ac.ui.cs.myui">
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
......@@ -13,6 +14,13 @@
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
<data
android:host="localhost"
android:scheme="akuncs"
android:path="/"/>
</intent-filter>
</activity>
<activity android:name=".activity.HomeActivity"/>
......
package id.ac.ui.cs.myui;
/**
* Created by hafiyyansayyidfadhlillah on 7/25/17.
*/
public class AccessToken {
private String access_token;
private String refresh_token;
private String token_type;
public String getAccessToken() {
return access_token;
}
public String getTokenType() {
return token_type;
}
@Override
public String toString(){
return access_token;
}
}
package id.ac.ui.cs.myui;
import java.io.IOException;
import okhttp3.Interceptor;
import okhttp3.Request;
import okhttp3.Response;
/**
* Created by hafiyyansayyidfadhlillah on 7/25/17.
*/
public class AuthenticationInterceptor implements Interceptor {
private String authToken;
public AuthenticationInterceptor(String token) {
this.authToken = token;
}
@Override
public Response intercept(Chain chain) throws IOException {
Request original = chain.request();
Request.Builder builder = original.newBuilder()
.header("Authorization", authToken);
Request request = builder.build();
return chain.proceed(request);
}
}
package id.ac.ui.cs.myui;
import java.util.List;
import retrofit2.Call;
import retrofit2.http.Field;
import retrofit2.http.FormUrlEncoded;
import retrofit2.http.GET;
import retrofit2.http.POST;
import retrofit2.http.Path;
import retrofit2.http.Query;
/**
* Created by hafiyyansayyidfadhlillah on 7/25/17.
*/
public interface LoginService {
@GET("token")
Call<AccessToken> getAccessToken(@Query("code") String code);
}
package id.ac.ui.cs.myui;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.os.AsyncTask;
import android.text.TextUtils;
import android.util.Log;
import com.google.gson.Gson;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.IOException;
import id.ac.ui.cs.myui.activity.HomeActivity;
import okhttp3.Credentials;
import okhttp3.OkHttpClient;
import retrofit2.Call;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;
/**
* Created by hafiyyansayyidfadhlillah on 7/25/17.
*/
public class ServiceGenerator extends AsyncTask<Object, Object, AccessToken> {
private Context context;
private String code;
public ServiceGenerator(Context context, String code){
this.context = context;
this.code = code;
}
public static final String API_BASE_URL = "https://akun.cs.ui.ac.id/";
public static final String BACKEND_BASE_URL = "http://10.0.2.2/";
@Override
protected AccessToken doInBackground(Object... objects) {
OkHttpClient.Builder httpClient = new OkHttpClient.Builder();
AccessToken data = null;
Retrofit.Builder builder =
new Retrofit.Builder()
.baseUrl(BACKEND_BASE_URL)
.addConverterFactory(GsonConverterFactory.create());
Retrofit retrofit = builder.build();
LoginService loginService = retrofit.create(LoginService.class);
Call<AccessToken> call = loginService.getAccessToken(code);
Response<AccessToken> body = null;
Log.i("CALL",call.request().url().toString());
try{
Log.i("FORM",call.request().body().contentType().toString());
}
catch (Exception e){
}
try{
body = call.execute();
String isSuccess = Integer.toString(body.code());
Log.i("SUCCESS", isSuccess);
Log.i("REASON", body.message());
JSONObject jsonResult = new JSONObject(new Gson().toJson(body.body()));
//JSONObject access_token = jsonResult.getJSONObject("access_token");
Log.i("TOKEN", jsonResult.toString());
}
catch (IOException e){
e.printStackTrace();
}
catch (JSONException e){
e.printStackTrace();
}
return data;
}
@Override
protected void onPostExecute(AccessToken accessToken) {
if(!(accessToken == null)){
Intent i = new Intent(context, HomeActivity.class);
context.startActivity(i);
}
}
}
......@@ -4,13 +4,23 @@ package id.ac.ui.cs.myui.activity;
* Created by hafiyyansayyidfadhlillah on 7/12/17.
*/
import android.content.Context;
import android.net.Uri;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.content.Intent;
import java.io.IOException;
import id.ac.ui.cs.myui.AccessToken;
import id.ac.ui.cs.myui.LoginService;
import id.ac.ui.cs.myui.R;
import id.ac.ui.cs.myui.ServiceGenerator;
import retrofit2.Call;
import retrofit2.http.Field;
/**
* This class defining Login page Logic
......@@ -18,8 +28,11 @@ import id.ac.ui.cs.myui.R;
*/
public class LoginActivity extends AppCompatActivity {
private final String clientId = "2bfJDNTlHhR9TJO6kQ5OsINqeMpNqFrudWOC8Vg9";
private final String redirectUri = "akuncs://localhost/";
@Override
protected void onCreate(Bundle savedInstanceState) {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
......@@ -30,9 +43,36 @@ public class LoginActivity extends AppCompatActivity {
Button loginButton = (Button) findViewById(R.id.login);
loginButton.setOnClickListener(new View.OnClickListener(){
public void onClick(View v){
Intent i = new Intent(LoginActivity.this, HomeActivity.class);
startActivity(i);
Intent intent = new Intent(
Intent.ACTION_VIEW,
Uri.parse(ServiceGenerator.API_BASE_URL + "" + "oauth/authorize/?response_type=code&client_id=" + clientId + "&state=random_state_string"));
startActivity(intent);
}
});
}
protected void onResume(){
super.onResume();
Log.i("MASUK","Masuk");
// the intent filter defined in AndroidManifest will handle the return from ACTION_VIEW intent
Uri uri = getIntent().getData();
if (uri != null && uri.toString().startsWith(redirectUri)) {
Log.i("URI",uri.toString());
// use the parameter your API exposes for the code (mostly it's "code")
String code = uri.getQueryParameter("code");
Log.i("CODE",code);
if (code != null) {
// get access token
try{
ServiceGenerator serviceGenerator = new ServiceGenerator(getApplicationContext(),code);
serviceGenerator.execute();
}
catch (Exception e){
e.printStackTrace();
}
} else if (uri.getQueryParameter("error") != null) {
// show an error message here
}
}
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment