Add innerParams.

This commit is contained in:
Yang Luo 2021-03-21 13:45:55 +08:00
parent 55b62e9786
commit 26502350d3
4 changed files with 31 additions and 24 deletions

View File

@ -12,8 +12,6 @@
// See the License for the specific language governing permissions and
// limitations under the License.
import {trim} from "./Util";
export let authConfig = {
serverUrl: "http://example.com", // your Casdoor URL, like the official one: https://door.casbin.com
appName: "app-example", // your Casdoor application name, like: "app-built-in"
@ -23,11 +21,3 @@ export let authConfig = {
export function initAuthWithConfig(config) {
authConfig = config;
}
export function getMyProfileUrl() {
return `${trim(authConfig.serverUrl)}/account`
}
export function getUserProfileUrl(userName) {
return `${trim(authConfig.serverUrl)}/users/${authConfig.organizationName}/${userName}`;
}

View File

@ -30,14 +30,20 @@ class AuthCallback extends React.Component {
};
}
getInnerParams() {
// For example, for Casbin-OA, realRedirectUri = "http://localhost:9000/login"
// realRedirectUrl = "http://localhost:9000"
const params = new URLSearchParams(this.props.location.search);
const state = params.get("state");
return new URLSearchParams(Util.stateToGetQueryParams(state));
}
getResponseType() {
// "http://localhost:8000"
const authServerUrl = authConfig.serverUrl;
// For example, for Casbin-OA, realRedirectUri = "http://localhost:9000/login"
// realRedirectUrl = "http://localhost:9000"
const params = new URLSearchParams(this.props.location.search);
const realRedirectUri = params.get("redirect_uri");
const innerParams = this.getInnerParams();
const realRedirectUri = innerParams.get("redirect_uri");
const realRedirectUrl = new URL(realRedirectUri).origin;
// For Casdoor itself, we use "login" directly
@ -50,17 +56,18 @@ class AuthCallback extends React.Component {
componentWillMount() {
const params = new URLSearchParams(this.props.location.search);
const innerParams = this.getInnerParams();
let redirectUri = `${window.location.origin}/callback/${this.state.applicationName}/${this.state.providerName}/${this.state.method}`;
const body = {
type: this.getResponseType(),
application: this.state.applicationName,
provider: this.state.providerName,
code: params.get("code"),
state: params.get("state"),
state: innerParams.get("state"),
redirectUri: redirectUri,
method: this.state.method,
};
const oAuthParams = Util.getOAuthGetParameters();
const oAuthParams = Util.getOAuthGetParameters(innerParams);
AuthBackend.login(body, oAuthParams)
.then((res) => {
if (res.status === 'ok') {

View File

@ -12,7 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.
const AuthState = "casdoor";
import * as Util from "./Util";
const GoogleAuthScope = "profile+email"
const GoogleAuthUri = "https://accounts.google.com/signin/oauth";
@ -43,14 +43,15 @@ export function getAuthLogo(provider) {
}
export function getAuthUrl(application, provider, method) {
const redirectUri = `${window.location.origin}/callback/${application.name}/${provider.name}/${method}${encodeURIComponent(window.location.search)}`;
const redirectUri = `${window.location.origin}/callback/${application.name}/${provider.name}/${method}`;
const state = Util.getQueryParamsToState();
if (provider.type === "google") {
return `${GoogleAuthUri}?client_id=${provider.clientId}&redirect_uri=${redirectUri}&scope=${GoogleAuthScope}&response_type=code&state=${AuthState}`;
return `${GoogleAuthUri}?client_id=${provider.clientId}&redirect_uri=${redirectUri}&scope=${GoogleAuthScope}&response_type=code&state=${state}`;
} else if (provider.type === "github") {
return `${GithubAuthUri}?client_id=${provider.clientId}&redirect_uri=${redirectUri}&scope=${GithubAuthScope}&response_type=code&state=${AuthState}`;
return `${GithubAuthUri}?client_id=${provider.clientId}&redirect_uri=${redirectUri}&scope=${GithubAuthScope}&response_type=code&state=${state}`;
} else if (provider.type === "qq") {
return `${QqAuthUri}?client_id=${provider.clientId}&redirect_uri=${redirectUri}&scope=${QqAuthScope}&response_type=code&state=${AuthState}`;
return `${QqAuthUri}?client_id=${provider.clientId}&redirect_uri=${redirectUri}&scope=${QqAuthScope}&response_type=code&state=${state}`;
} else if (provider.type === "wechat") {
return `${WeChatAuthUri}?appid=${provider.clientId}&redirect_uri=${redirectUri}&scope=${WeChatAuthScope}&response_type=code&state=${AuthState}#wechat_redirect`;
return `${WeChatAuthUri}?appid=${provider.clientId}&redirect_uri=${redirectUri}&scope=${WeChatAuthScope}&response_type=code&state=${state}#wechat_redirect`;
}
}

View File

@ -43,8 +43,8 @@ export function trim(str, ch) {
return (start > 0 || end < str.length) ? str.substring(start, end) : str;
}
export function getOAuthGetParameters() {
const queries = new URLSearchParams(window.location.search);
export function getOAuthGetParameters(params) {
const queries = (params !== undefined) ? params : new URLSearchParams(window.location.search);
const clientId = queries.get("client_id");
const responseType = queries.get("response_type");
const redirectUri = queries.get("redirect_uri");
@ -62,3 +62,12 @@ export function getOAuthGetParameters() {
};
}
}
export function getQueryParamsToState() {
const query = window.location.search;
return btoa(query);
}
export function stateToGetQueryParams(state) {
return atob(state);
}