Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

OAuth2 support (fix #37) #41

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -242,12 +242,14 @@ private class UserLoginTask extends AsyncTask<Void, String, ErrorMessage> {
private final String mUri;
private final String mIpAddress;
private final String mPassword;
private final String mBearerHeader;
private String mBoostrapData;

UserLoginTask(String uri, String ipAddress, String password) {
mUri = uri;
mIpAddress = ipAddress;
mPassword = password;
mBearerHeader = "Bearer " + password;
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I an not familiar with this code base, so this may be a dumb question; could this be replaced with a call to getBearerHeader()?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, it is not possible here.


mIpAddressView.setEnabled(false);
mPasswordView.setEnabled(false);
Expand All @@ -261,8 +263,8 @@ protected ErrorMessage doInBackground(Void... params) {
try {
publishProgress(getString(R.string.progress_connecting));

//Response<BootstrapResponse> response = ServiceProvider.getApiService(mUri).bootstrap(mPassword).execute();
Response<String> response = ServiceProvider.getRawApiService(mUri).rawStates(mPassword).execute();
//Response<BootstrapResponse> response = ServiceProvider.getApiService(mUri).bootstrap(mBearerHeader).execute();
Response<String> response = ServiceProvider.getRawApiService(mUri).rawStates(mBearerHeader).execute();

if (response.code() != 200) {
if (response.code() == 401) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ public void refreshApi() {

if (mCall == null) {
//showNetworkBusy();
mCall = ServiceProvider.getApiService(mCurrentServer.getBaseUrl()).getLogbook(mCurrentServer.getPassword(), df.format(now.getTime()));
mCall = ServiceProvider.getApiService(mCurrentServer.getBaseUrl()).getLogbook(mCurrentServer.getBearerHeader(), df.format(now.getTime()));
mCall.enqueue(new Callback<ArrayList<LogSheet>>() {
@Override
public void onResponse(@NonNull Call<ArrayList<LogSheet>> call, @NonNull Response<ArrayList<LogSheet>> response) {
Expand Down
6 changes: 3 additions & 3 deletions app/src/main/java/com/axzae/homeassistant/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -718,7 +718,7 @@ public void onClick(@NonNull MaterialDialog dialog, @NonNull DialogAction which)

if (mCall2 == null) {
showNetworkBusy();
mCall2 = ServiceProvider.getRawApiService(mCurrentServer.getBaseUrl()).rawStates(mCurrentServer.getPassword());
mCall2 = ServiceProvider.getRawApiService(mCurrentServer.getBaseUrl()).rawStates(mCurrentServer.getBearerHeader());
mCall2.enqueue(new Callback<String>() {
@Override
public void onResponse(@NonNull Call<String> call, @NonNull Response<String> response) {
Expand Down Expand Up @@ -874,7 +874,7 @@ public void callService(final String domain, final String service, CallServiceRe
Log.d("YouQi", "Using HTTP");
showNetworkBusy();
Crashlytics.log("baseUrl: " + mCurrentServer.getBaseUrl());
mCall = ServiceProvider.getApiService(mCurrentServer.getBaseUrl()).callService(mCurrentServer.getPassword(), domain, service, serviceRequest);
mCall = ServiceProvider.getApiService(mCurrentServer.getBaseUrl()).callService(mCurrentServer.getBearerHeader(), domain, service, serviceRequest);
mCall.enqueue(new Callback<ArrayList<Entity>>() {
@Override
public void onResponse(@NonNull Call<ArrayList<Entity>> call, @NonNull Response<ArrayList<Entity>> response) {
Expand Down Expand Up @@ -979,7 +979,7 @@ private class RefreshTask extends AsyncTask<Void, String, ErrorMessage> {
protected ErrorMessage doInBackground(Void... params) {
try {
publishProgress("Connecting");
Response<ArrayList<Entity>> response = ServiceProvider.getApiService(mCurrentServer.getBaseUrl()).getStates(mCurrentServer.getPassword()).execute();
Response<ArrayList<Entity>> response = ServiceProvider.getApiService(mCurrentServer.getBaseUrl()).getStates(mCurrentServer.getBearerHeader()).execute();
if (response.code() != 200) {
//OAuthToken token = new Gson().fromJson(response.errorBody().string(), OAuthToken.class);
return new ErrorMessage("Error", response.message());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ protected void onCreate(Bundle savedInstanceState) {
EntityHandlerHelper.onEntityLongClick(this, entity);
}

Call<Entity> mCall = ServiceProvider.getApiService(mCurrentServer.getBaseUrl()).getState(mCurrentServer.getPassword(), mEntity.entityId);
Call<Entity> mCall = ServiceProvider.getApiService(mCurrentServer.getBaseUrl()).getState(mCurrentServer.getBearerHeader(), mEntity.entityId);
mCall.enqueue(new Callback<Entity>() {
@Override
public void onResponse(@NonNull Call<Entity> call, @NonNull Response<Entity> response) {
Expand Down Expand Up @@ -151,7 +151,7 @@ public void callService(final String domain, final String service, CallServiceRe
Log.d("YouQi", String.format(Locale.ENGLISH, "callService(%s, %s) in TransparentActivity: %s", domain, service, CommonUtil.deflate(serviceRequest)));
if (mCall == null) {
//showNetworkBusy();
mCall = ServiceProvider.getApiService(mCurrentServer.getBaseUrl()).callService(mCurrentServer.getPassword(), domain, service, serviceRequest);
mCall = ServiceProvider.getApiService(mCurrentServer.getBaseUrl()).callService(mCurrentServer.getBearerHeader(), domain, service, serviceRequest);
mCall.enqueue(new Callback<ArrayList<Entity>>() {
@Override
public void onResponse(@NonNull Call<ArrayList<Entity>> call, @NonNull Response<ArrayList<Entity>> response) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ private void attemptLogin() {
}

private void testConnection(final HomeAssistantServer connection) {
ServiceProvider.getRawApiService(connection.getBaseUrl()).rawStates(connection.getPassword())
ServiceProvider.getRawApiService(connection.getBaseUrl()).rawStates(connection.getBearerHeader())
.enqueue(new Callback<String>() {
@Override
public void onResponse(@NonNull Call<String> call, @NonNull Response<String> response) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ public Dialog onCreateDialog(Bundle savedInstanceState) {
public void callService() {
if (mCall == null) {
mProgressBar.setVisibility(View.VISIBLE);
mCall = ServiceProvider.getApiService(mServer.getBaseUrl()).getHistory(mServer.getPassword(), mEntity.entityId);
mCall = ServiceProvider.getApiService(mServer.getBaseUrl()).getHistory(mServer.getBearerHeader(), mEntity.entityId);
mCall.enqueue(new Callback<ArrayList<ArrayList<Entity>>>() {
@Override
public void onResponse(@NonNull Call<ArrayList<ArrayList<Entity>>> call, @NonNull Response<ArrayList<ArrayList<Entity>>> response) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ public Dialog onCreateDialog(Bundle savedInstanceState) {
public void callService() {
if (mCall == null) {
mProgressBar.setVisibility(View.VISIBLE);
mCall = ServiceProvider.getApiService(mServer.getBaseUrl()).getHistory(mServer.getPassword(), mEntity.entityId);
mCall = ServiceProvider.getApiService(mServer.getBaseUrl()).getHistory(mServer.getBearerHeader(), mEntity.entityId);
mCall.enqueue(new Callback<ArrayList<ArrayList<Entity>>>() {
@Override
public void onResponse(@NonNull Call<ArrayList<ArrayList<Entity>>> call, @NonNull Response<ArrayList<ArrayList<Entity>>> response) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,10 @@ public String getPassword() {
return password;
}

public String getBearerHeader() {
return "Bearer " + password;
}

public String getWebsocketUrl() {
return "ws" + getBaseUrl().substring(4) + "/api/websocket";
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ public void onUpdate(final Context context, final AppWidgetManager appWidgetMana
//updateEntityWidget(context, widget);

if (mCurrentServer != null) {
Call<Entity> mCall = ServiceProvider.getApiService(mCurrentServer.getBaseUrl()).getState(mCurrentServer.getPassword(), widget.entityId);
Call<Entity> mCall = ServiceProvider.getApiService(mCurrentServer.getBaseUrl()).getState(mCurrentServer.getBearerHeader(), widget.entityId);
mCall.enqueue(new Callback<Entity>() {
@Override
public void onResponse(@NonNull Call<Entity> call, @NonNull Response<Entity> response) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,28 +25,28 @@
public interface RestAPIService {

@GET("/api/bootstrap")
Call<BootstrapResponse> bootstrap(@Header("x-ha-access") String password);
Call<BootstrapResponse> bootstrap(@Header("Authorization") String password);

@GET("/api/bootstrap")
Call<String> rawBootstrap(@Header("x-ha-access") String password);
Call<String> rawBootstrap(@Header("Authorization") String password);

@GET("/api/states")
Call<String> rawStates(@Header("x-ha-access") String password);
Call<String> rawStates(@Header("Authorization") String password);

@GET("/api/states")
Call<ArrayList<Entity>> getStates(@Header("x-ha-access") String password);
Call<ArrayList<Entity>> getStates(@Header("Authorization") String password);

@GET("/api/states/{entityId}")
Call<Entity> getState(@Header("x-ha-access") String password, @Path("entityId") String entityId);
Call<Entity> getState(@Header("Authorization") String password, @Path("entityId") String entityId);

@POST("/api/services/{domain}/{service}")
Call<ArrayList<Entity>> callService(@Header("x-ha-access") String password, @Path("domain") String domain, @Path("service") String service, @Body CallServiceRequest json);
Call<ArrayList<Entity>> callService(@Header("Authorization") String password, @Path("domain") String domain, @Path("service") String service, @Body CallServiceRequest json);

@GET("/api/history/period")
Call<ArrayList<ArrayList<Entity>>> getHistory(@Header("x-ha-access") String password, @Query("filter_entity_id") String entityId);
Call<ArrayList<ArrayList<Entity>>> getHistory(@Header("Authorization") String password, @Query("filter_entity_id") String entityId);

@GET("/api/logbook/{timestamp}")
Call<ArrayList<LogSheet>> getLogbook(@Header("x-ha-access") String password, @Path("timestamp") String domain);
Call<ArrayList<LogSheet>> getLogbook(@Header("Authorization") String password, @Path("timestamp") String domain);

// @GET("api/settings/all")
// Call<RetrieveSettingsResponse> getSettings(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@

public class ServiceProvider {
private static final int CONNECTION_TIMEOUT_IN_SEC = 5;
private static final int DEFAULT_READ_TIMEOUT_IN_SEC = 15;
private static final int DEFAULT_READ_TIMEOUT_IN_SEC = 30;
private static OkHttpClient okHttpClient;
private static OkHttpClient wsOkHttpClient;
private static OkHttpClient glideOkHttpClient;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,7 @@ public void processResponse(String s) {
case "auth_required":
sendCommand(new JSONObject()
.put("type", "auth")
.put("api_password", mPassword)
.put("access_token", mPassword)
.toString());
break;
case "auth_ok":
Expand Down