Skip to content

Commit

Permalink
Merge pull request Azure#11 from gcheng/nimbusauth
Browse files Browse the repository at this point in the history
Implementation of Authentication for Media Service.
  • Loading branch information
Albert Cheng committed Aug 15, 2012
2 parents 17cf30a + 4ea62a2 commit 15a9352
Show file tree
Hide file tree
Showing 10 changed files with 883 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
/**
* Copyright 2011 Microsoft Corporation
*
* 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.microsoft.windowsazure.services.media;

import java.util.Date;

/**
* A class representing active token for OAuthTokenResponse.
*
* @author azurejava@microsoft.com
*
*/
public class ActiveToken {

private Date expiresUtc;
private OAuthTokenResponse oAuthTokenResponse;

/**
* Gets the expiration time in UTC.
*
* @return The token expiration time in UTC.
*/
public Date getExpiresUtc() {
return expiresUtc;
}

/**
* Sets the token expiration time in UTC.
*
* @param expiresUtc
*/
public void setExpiresUtc(Date expiresUtc) {
this.expiresUtc = expiresUtc;
}

/**
* Gets the OAuth token response.
*
* @return The OAuth token response.
*/
public OAuthTokenResponse getOAuthTokenResponse() {
return oAuthTokenResponse;
}

/**
* Sets the OAuth token response.
*
* @param oAuth2TokenResponse
*/
public void setOAuth2TokenResponse(OAuthTokenResponse oAuth2TokenResponse) {
this.oAuthTokenResponse = oAuth2TokenResponse;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/**
* Copyright 2011 Microsoft Corporation
*
* 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.microsoft.windowsazure.services.media;

import com.microsoft.windowsazure.services.core.Builder;

public class Exports implements Builder.Exports {

/**
* register the OAUTH service.
*/
@Override
public void register(Builder.Registry registry) {
registry.add(OAuthContract.class, OAuthRestProxy.class);
registry.add(OAuthTokenManager.class);
registry.add(OAuthFilter.class);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,155 @@
/**
* Copyright 2011 Microsoft Corporation
*
* 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.microsoft.windowsazure.services.media;

import com.microsoft.windowsazure.services.core.Configuration;

/**
* Provides functionality to create a service bus configuration.
*
*/
public class MediaConfiguration {

/**
* Defines the media service configuration URI constant.
*
*/
public static final String URI = "media.uri";

/**
* Defines the OAUTH configuration URI constant.
*
*/
public static final String OAUTH_URI = "oauth.uri";

/**
* Defines the OAUTH configuration client ID constant.
*
*/
public static final String OAUTH_CLIENT_ID = "oauth.client.id";

/**
* Defines the OAUTH configuration client secret constant.
*
*/
public static final String OAUTH_CLIENT_SECRET = "oauth.client.secret";

/**
* Defines the SCOPE of the media service sent to OAUTH.
*/
public static final String OAUTH_SCOPE = "oauth.scope";

/**
* Creates a media service configuration using the specified media service base URI, OAUTH URI,
* client ID, and client secret.
*
* @param mediaServiceBaseUri
* A <code>String</code> object that represents the media service base URI.
*
* @param oAuthUri
* A <code>String</code> object that represents the OAUTH URI.
*
* @param clientId
* A <code>String</code> object that represents the client ID.
*
* @param clientSecret
* A <code>String</code> object that represents the client secret.
*
* @return
* A <code>Configuration</code> object that can be used when creating an instance of the
* <code>MediaService</code> class.
*
*/
public static Configuration configureWithOAuthAuthentication(String mediaServiceBaseUri, String oAuthUri,
String clientId, String clientSecret) {
return configureWithOAuthAuthentication(null, Configuration.getInstance(), mediaServiceBaseUri, oAuthUri,
clientId, clientSecret);
}

/**
* Creates a media service configuration using the specified configuration, media service base URI, OAuth URI,
* client ID, and client secret.
*
* @param configuration
* A previously instantiated <code>Configuration</code> object.
*
* @param mediaServiceBaseUri
* A <code>String</code> object that represents the base URI of Media service.
*
* @param oAuthUri
* A <code>String</code> object that represents the URI of OAuth service.
*
* @param clientId
* A <code>String</code> object that represents the client ID.
*
* @param clientSecret
* A <code>String</code> object that represents the client secret.
*
* @return
* A <code>Configuration</code> object that can be used when creating an instance of the
* <code>MediaService</code> class.
*
*/
public static Configuration configureWithOAuthAuthentication(Configuration configuration,
String mediaServiceBaseUri, String oAuthUri, String clientId, String clientSecret) {
return configureWithOAuthAuthentication(null, configuration, mediaServiceBaseUri, oAuthUri, clientId,
clientSecret);
}

/**
* Creates a media service configuration using the specified profile, configuration, media service base URI,
* OAuth URI, client ID, and client secret.
*
* @param profile
* A <code>String</code> object that represents the profile.
*
* @param configuration
* A previously instantiated <code>Configuration</code> object.
*
* @param mediaServiceBaseUri
* A <code>String</code> object that represents the base URI of media service.
*
* @param oAuthUri
* A <code>String</code> object that represents the URI of OAUTH service.
*
* @param clientId
* A <code>String</code> object that represents the client ID.
*
* @param clientSecret
* A <code>String</code> object that represents the client secret.
*
* @return
* A <code>Configuration</code> object that can be used when creating an instance of the
* <code>MediaService</code> class.
*
*/
public static Configuration configureWithOAuthAuthentication(String profile, Configuration configuration,
String mediaServiceBaseUri, String oAuthUri, String clientId, String clientSecret) {

if (profile == null) {
profile = "";
}
else if (profile.length() != 0 && !profile.endsWith(".")) {
profile = profile + ".";
}

configuration.setProperty(profile + URI, "https://" + mediaServiceBaseUri);
configuration.setProperty(profile + OAUTH_URI, oAuthUri);
configuration.setProperty(profile + OAUTH_CLIENT_ID, clientId);
configuration.setProperty(profile + OAUTH_CLIENT_SECRET, clientSecret);

return configuration;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/**
* Copyright 2011 Microsoft Corporation
*
* 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.microsoft.windowsazure.services.media;

import java.net.URI;

import com.microsoft.windowsazure.services.core.ServiceException;

public interface OAuthContract {
/**
* Gets an OAuth access token with specified OAUTH URI, client ID, client secret, and scope.
*
* @param oAuthUri
* A <code>URI</code> object which represents an OAUTH URI.
*
* @param clientId
* A <code>String</code> object which represents a client ID.
*
* @param clientSecret
* A <code>String</code> object which represents a client secret.
*
* @param scope
* A <code>String</code> object which represents the scope.
*
* @return OAuthTokenResponse
* @throws ServiceException
*/
public OAuthTokenResponse getAccessToken(URI oAuthUri, String clientId, String clientSecret, String scope)
throws ServiceException;

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/*
* Copyright 2011 Microsoft Corporation
*
* 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.microsoft.windowsazure.services.media;

import java.net.URISyntaxException;

import com.microsoft.windowsazure.services.core.ServiceException;
import com.sun.jersey.api.client.ClientHandlerException;
import com.sun.jersey.api.client.ClientRequest;
import com.sun.jersey.api.client.ClientResponse;
import com.sun.jersey.api.client.filter.ClientFilter;

public class OAuthFilter extends ClientFilter {
private final OAuthTokenManager oAuthTokenManager;

/**
* Creates an <code>OAuthFilter</code> object with specfied <code>OAuthTokenManager</code> instance.
*
* @param oAuthTokenManager
*/
public OAuthFilter(OAuthTokenManager oAuthTokenManager) {
this.oAuthTokenManager = oAuthTokenManager;
}

/**
* Handles response with a specified client request.
*
* @param clientRequest
* A <code>ClientRequest</code> object representing a client request.
*/
@Override
public ClientResponse handle(ClientRequest clientRequest) throws ClientHandlerException {

String accessToken;
try {
accessToken = oAuthTokenManager.getAccessToken(clientRequest.getURI().toString());
}
catch (ServiceException e) {
// must wrap exception because of base class signature
throw new ClientHandlerException(e);
}
catch (URISyntaxException e) {
// must wrap exception because of base class signature
throw new ClientHandlerException(e);
}

clientRequest.getHeaders().add("Authorization", "Bearer " + accessToken);

return this.getNext().handle(clientRequest);
}
}
Loading

0 comments on commit 15a9352

Please sign in to comment.