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

Fix Export, expand instructions, add Cloudformation script #223

Open
wants to merge 53 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
53 commits
Select commit Hold shift + click to select a range
67aaec8
Expand instructions. Add Cloudfront.
brunosan May 26, 2015
3d0314d
Rename CloudFront -> CloudFormation
brunosan May 26, 2015
d17fbf8
Add cloud formation to run on port 80
brunosan May 26, 2015
decdcab
initial work on auth0
landonreed Jan 27, 2016
d76b8aa
work on auth0 login
Jan 28, 2016
cb6a47e
further auth0 updates
landonreed Jan 28, 2016
c545578
pull agencies from manager
Feb 7, 2016
4ed616a
SSO support
Feb 8, 2016
8537b70
fix logout
Feb 8, 2016
f99d9fb
Nav bar layout
Feb 9, 2016
c88f5b2
Fix users link
Feb 9, 2016
2e87abd
Read project settings from manager when importing feed
Feb 10, 2016
7615ae7
remove obsolete code
Feb 12, 2016
59cf6f0
use window.location.replace()
Feb 12, 2016
83b78ca
fix redirect loop
Feb 12, 2016
f9eafa7
Single logout support + navbar restructuring
Feb 15, 2016
ad504c6
Specify manager, useradmin URLs in application.conf
Feb 16, 2016
e6899ad
fix single logout bug
Feb 16, 2016
1af74bd
use application.conf auth settings throughout application
Feb 16, 2016
d4b9ee6
Redirect to auth0logout on auth failure
Feb 16, 2016
5bde16f
Auth0 lock config
Feb 18, 2016
813c003
New user permission format
Feb 26, 2016
112b722
Manager/Editor integration
Mar 8, 2016
5a2c8c0
Integration with migrated datatools-manager
Mar 30, 2016
c4f0c27
Don't filter manager feeds by data availability
Apr 1, 2016
52263e8
Import existing GTFS from Manager
Apr 21, 2016
75be1ef
updates to navbar
landonreed Apr 21, 2016
f784e5d
Merge branch 'new-mgr-integration' of github.com:conveyal/gtfs-editor…
landonreed Apr 21, 2016
15037a6
fixed users url
landonreed Apr 21, 2016
ea2d702
update manager link to project
landonreed Apr 21, 2016
d4628b8
updated Auth0UserProfile to include subscriptions
landonreed May 4, 2016
01e2f17
disable signup action
landonreed May 4, 2016
b25a9c5
fixed auth issue
landonreed May 11, 2016
c775db1
Upgrade Auth0 lock library
May 14, 2016
ecb5c49
fixed issue with default feeds
landonreed May 19, 2016
39c2fb5
Editor permissions
May 23, 2016
d1e35e4
Remove debugging logs
May 23, 2016
43211e5
Show feed pill bar only when appropriate
May 23, 2016
4567d00
Disable single signout check
May 23, 2016
2bc8bcf
Disable change password
May 23, 2016
e01b034
fix for starting from blank db
landonreed Jun 8, 2016
a85da63
Merge branch 'new-mgr-integration' of github.com:conveyal/gtfs-editor…
landonreed Jun 8, 2016
9662c44
merge upstream conveyal/master
brunosan Jul 21, 2016
212a4f2
Solve merge conflict
brunosan Jul 21, 2016
e9792ae
fix export error.
brunosan Jul 21, 2016
892be34
rename file for clarity
brunosan Jul 21, 2016
ae433d4
fix links on instructions
brunosan Jul 21, 2016
c94c7f8
Add public/data fix on cloudformation template level
brunosan Jul 21, 2016
98e3f3c
merge from new-mgr-integration
brunosan Sep 14, 2016
291357d
deploy pointer to dev: conveyal->bruno
brunosan Sep 14, 2016
26bf475
Merge from @5Tsrl ba136fcb7f41758ba95e8c5d5d8847ff5b8f5f99
brunosan Sep 14, 2016
4306782
ignore DS_Store
brunosan Sep 15, 2016
30111dd
Actually install openjdk
brunosan Sep 15, 2016
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
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
.DS_Store
bin
conf/application.conf
precompiled
Expand All @@ -14,4 +15,4 @@ eclipse
test-result
dropAndRecreateDatabase.sh
dump
.idea
.idea
Binary file added app/.DS_Store
Binary file not shown.
120 changes: 120 additions & 0 deletions app/controllers/Auth0Controller.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
package controllers;

import com.fasterxml.jackson.databind.ObjectMapper;

import play.Play;
import play.mvc.Controller;
import utils.Auth0UserProfile;

import javax.net.ssl.HttpsURLConnection;

import org.apache.commons.lang.StringUtils;

import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.lang.Exception;
import java.net.URL;
import java.util.regex.Pattern;

public class Auth0Controller extends Controller {
public static void auth0Login(String token) {
System.out.println("auth0Login token="+token);

// get user profile from Auth0 tokeninfo API

session.put("token", token);

try {
Auth0UserProfile profile = getUserInfo(token);
session.put("username", profile.getEmail());

String projectID = Play.configuration.getProperty("application.projectId");

String editableFeeds = StringUtils.join(profile.getEditableFeeds(projectID), ",");
session.put("editableFeeds", editableFeeds);

String manageableFeeds = StringUtils.join(profile.getManageableFeeds(projectID), ",");
session.put("manageableFeeds", manageableFeeds);

String approveableFeeds = StringUtils.join(profile.getApproveableFeeds(projectID), ",");
session.put("approveableFeeds", approveableFeeds);

String isProjectAdmin = profile.canAdministerProject(projectID) ? "true" : "false";
session.put("isProjectAdmin", isProjectAdmin);

}
catch (Exception e) {
e.printStackTrace();
System.out.println("Error in user auth, redirecting to /auth0logout");
redirect("/auth0logout");
}

ok();
}

public static void auth0Logout(String token) {
System.out.println("logging out");
session.clear();
redirect("/");
}

protected static String getToken() {
String token = null;
final String authorizationHeader = request.params.get("authorization");
if (authorizationHeader == null) return null;

// check format (Authorization: Bearer [token])
String[] parts = authorizationHeader.split(" ");
if (parts.length != 2) return null;

String scheme = parts[0];
String credentials = parts[1];

Pattern pattern = Pattern.compile("^Bearer$", Pattern.CASE_INSENSITIVE);
if (pattern.matcher(scheme).matches()) {
token = credentials;
}
return token;
}

protected static Auth0UserProfile getUserInfo(String token) throws Exception {

URL url = new URL("https://" + Play.configuration.getProperty("application.auth0Domain") + "/tokeninfo");
HttpsURLConnection con = (HttpsURLConnection) url.openConnection();

//add request header
con.setRequestMethod("POST");
con.setRequestProperty("User-Agent", "USER_AGENT");
con.setRequestProperty("Accept-Language", "en-US,en;q=0.5");

String urlParameters = "id_token=" + token;

// Send post request
con.setDoOutput(true);
DataOutputStream wr = new DataOutputStream(con.getOutputStream());
wr.writeBytes(urlParameters);
wr.flush();
wr.close();
InputStreamReader is = new InputStreamReader(con.getInputStream());
if (is == null) {
return null;
}
BufferedReader in = new BufferedReader(is);
String inputLine;
StringBuffer response = new StringBuffer();

while ((inputLine = in.readLine()) != null) {
response.append(inputLine);
}
in.close();

String userInfo = response.toString();

ObjectMapper m = new ObjectMapper();
return m.readValue(userInfo, Auth0UserProfile.class);


}
}
118 changes: 118 additions & 0 deletions app/controllers/api/ManagerApiController.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
package controllers.api;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import jobs.ProcessGtfsSnapshotExport;
import models.Snapshot;
import models.transit.Agency;
import models.transit.Stop;

import org.mapdb.Fun;
import org.mapdb.Fun.Tuple2;

import controllers.Base;
import controllers.Application;
import controllers.Secure;
import controllers.Security;
import datastore.GlobalTx;
import datastore.VersionedDataStore;
import play.Logger;
import play.Play;
import play.mvc.Before;
import play.mvc.Controller;
import play.mvc.With;
import play.mvc.Http;

import utils.JacksonSerializers;

public class ManagerApiController extends Controller {

// todo: Auth0 authentication

@Before
public static void setCORS() {
Http.Header origin = new Http.Header();
origin.name = "Access-Control-Allow-Origin";
origin.values = new ArrayList<String>();
origin.values.add("*");
Http.Response.current().headers.put("Access-Control-Allow-Origin",origin);

Http.Header headers = new Http.Header();
headers.name = "Access-Control-Allow-Headers";
headers.values = new ArrayList<String>();
headers.values.add("Origin, X-Requested-With, Content-Type, Accept, Authorization");
Http.Response.current().headers.put("Access-Control-Allow-Headers",headers);
}

public static void options() {
}

public static void getSnapshot(String sourceId, String id) throws IOException {
GlobalTx gtx = VersionedDataStore.getGlobalTx();

System.out.println("getSnapshot for " +sourceId);
try {
if (id != null) {
Tuple2<String, Integer> sid = JacksonSerializers.Tuple2IntDeserializer.deserialize(id);
if (gtx.snapshots.containsKey(sid))
renderJSON(Base.toJson(gtx.snapshots.get(sid), false));
else
notFound();

return;
} else {
Collection<Snapshot> snapshots = new ArrayList<>();
Collection<Snapshot> allSnapshots;

allSnapshots = gtx.snapshots.values();
for(Snapshot snapshot : allSnapshots) {
Agency agency = gtx.agencies.get(snapshot.agencyId);
if(agency == null || agency.sourceId == null) continue;
if(agency.sourceId.equals(sourceId)) {
System.out.println("found!");
snapshots.add(snapshot);
}
}

renderJSON(Base.toJson(snapshots, false));
}
} finally {
gtx.rollback();
}
}

/** Export a snapshot as GTFS */
public static void exportSnapshot (String id) {
Tuple2<String, Integer> decodedId;
try {
decodedId = JacksonSerializers.Tuple2IntDeserializer.deserialize(id);
} catch (IOException e1) {
badRequest();
return;
}

GlobalTx gtx = VersionedDataStore.getGlobalTx();
Snapshot local;
try {
if (!gtx.snapshots.containsKey(decodedId)) {
notFound();
return;
}

local = gtx.snapshots.get(decodedId);

File out = new File(Play.configuration.getProperty("application.publicDataDirectory"), "gtfs_" + Application.nextExportId.incrementAndGet() + ".zip");

new ProcessGtfsSnapshotExport(local, out).run();

redirect(Play.configuration.getProperty("application.appBase") + "/public/data/" + out.getName());
} finally {
gtx.rollbackIfOpen();
}
}

}
Loading