Welcome to mirror list, hosted at ThFree Co, Russian Federation.

GitHubAuthServlet.java « online « mxgraph « com « java « main « src - github.com/jgraph/drawio.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: c1758fe77fc5d24bf1e57253689919ceabf786fc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
/**
 * Copyright (c) 2006-2019, JGraph Ltd
 */
package com.mxgraph.online;

import java.io.IOException;

@SuppressWarnings("serial")
public class GitHubAuthServlet extends AbsAuthServlet
{
	public static String CLIENT_SECRET_FILE_PATH = "github_client_secret";
	public static String CLIENT_ID_FILE_PATH = "github_client_id";
	public static String AUTH_SERVICE_URL_FILE_PATH = "github_auth_url";
	
	private static Config CONFIG = null;
	
	protected Config getConfig()
	{
		if (CONFIG == null)
		{
			String clientSerets, clientIds;
			
			try
			{
				clientSerets = Utils
						.readInputStream(getServletContext()
								.getResourceAsStream(getSecretPath()))
						.replaceAll("\n", "");
			}
			catch (IOException e)
			{
				throw new RuntimeException("Client secrets path invalid");
			}

			try
			{
				clientIds = Utils
						.readInputStream(getServletContext()
								.getResourceAsStream(getIdPath()))
						.replaceAll("\n", "");
			}
			catch (IOException e)
			{
				throw new RuntimeException("Client IDs path invalid");
			}
			
			CONFIG = new Config(clientIds, clientSerets);

			try
			{
				CONFIG.AUTH_SERVICE_URL = Utils
						.readInputStream(getServletContext()
								.getResourceAsStream(getServiceUrlPath()))
						.replaceAll("\n", "");
			}
			catch (IOException e)
			{
				CONFIG.AUTH_SERVICE_URL = "https://github.com/login/oauth/access_token";
			}
			
			CONFIG.REDIRECT_PATH = "/github2";
		}
		
		return CONFIG;
	}

	protected String getSecretPath()
	{
		return AbsAuthServlet.SECRETS_DIR_PATH + CLIENT_SECRET_FILE_PATH;
	}

	protected String getIdPath()
	{
		return AbsAuthServlet.SECRETS_DIR_PATH + CLIENT_ID_FILE_PATH;
	}

	protected String getServiceUrlPath()
	{
		return AbsAuthServlet.SECRETS_DIR_PATH + AUTH_SERVICE_URL_FILE_PATH;
	}

	public GitHubAuthServlet()
	{
		super();
		cookiePath = "/github2";
		withRedirectUrl = false;
		withAcceptJsonHeader = true;
	}
	
	protected String processAuthResponse(String authRes, boolean jsonResponse)
	{
		StringBuffer res = new StringBuffer();
		
		if (!jsonResponse)
		{
			res.append("<!DOCTYPE html><html><head><script type=\"text/javascript\">");
			res.append("(function() { var authInfo = ");  //The following is a json containing access_token
		}
		
		res.append(authRes);
		
		if (!jsonResponse)
		{
			res.append(";");
			res.append("if (window.opener != null && window.opener.onGitHubCallback != null)"); 
			res.append("{");
			res.append("	window.opener.onGitHubCallback(authInfo, window);");
			res.append("} else {");
			res.append("	onGitHubCallback(authInfo);");
			res.append("}");
			res.append("})();</script>");
			res.append("</head><body></body></html>");
		}

		return res.toString();
	}
}