/* * Copyright 2012 Nodeable Inc * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package com.streamreduce.oauth.api; import org.scribe.builder.api.DefaultApi20; import org.scribe.model.OAuthConfig; import org.scribe.utils.OAuthEncoder; import org.scribe.utils.Preconditions; public class GitHubApi extends DefaultApi20 { private static final String AUTHORIZE_URL = "https://github.com/login/oauth/authorize?client_id=%s&redirect_uri=%s"; @SuppressWarnings("unused") private static final String SCOPED_AUTHORIZE_URL = AUTHORIZE_URL + "&scope=%s"; private static final String ACCESS_TOKEN_URL = "https://github.com/login/oauth/access_token"; @Override public String getAccessTokenEndpoint() { return ACCESS_TOKEN_URL; } @Override public String getAuthorizationUrl(OAuthConfig config) { Preconditions.checkValidUrl(config.getCallback(), "Must provide a valid url as callback."); // Append scope if present if (config.hasScope()) { return String.format(SCOPED_AUTHORIZE_URL, config.getApiKey(), OAuthEncoder.encode(config.getCallback()), OAuthEncoder.encode(config.getScope())); } else { return String.format(AUTHORIZE_URL, config.getApiKey(), OAuthEncoder.encode(config.getCallback())); } } }