Skip to content

Instantly share code, notes, and snippets.

@jampajeen
Created February 20, 2016 04:00
Show Gist options
  • Save jampajeen/221d506807395e1b7720 to your computer and use it in GitHub Desktop.
Save jampajeen/221d506807395e1b7720 to your computer and use it in GitHub Desktop.
Generate oauth token
/*
* Copyright 2016 Thitipong Jampajeen <jampajeen@gmail.com>.
*
* 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.volho.smerp.web.service;
import com.volho.smerp.web.security.Authorities;
import java.security.Principal;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Set;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.oauth2.provider.endpoint.TokenEndpoint;
import org.springframework.stereotype.Component;
import org.springframework.web.HttpRequestMethodNotSupportedException;
/**
*
* @author Thitipong Jampajeen <jampajeen@gmail.com>
*
* This class use TokenEndpoint object from spring context directly, it's not recommended.
*/
@Component
public class InternalAuthTokenGenerator {
private static final Logger LOG = LoggerFactory.getLogger(InternalAuthTokenGenerator.class);
@Autowired
Config config;
@Autowired
private TokenEndpoint tokenEndpoint;
public String authenticateAndGetToken() {
try {
Set<GrantedAuthority> authorities = new HashSet<GrantedAuthority>();
authorities.add(new SimpleGrantedAuthority(Authorities.ROLE_USER.toString()));
UsernamePasswordAuthenticationToken authentication = new UsernamePasswordAuthenticationToken(config.OAUTH_COMMON_REMOTE_CLIENTID, config.OAUTH_COMMON_REMOTE_SECRET, authorities);
SecurityContextHolder.getContext().setAuthentication(authentication);
Principal pl = (Principal) authentication;
HashMap<String, String> parameters = new HashMap<String, String>();
parameters.put("grant_type", "client_credentials");
ResponseEntity obj = tokenEndpoint.getAccessToken(pl, parameters);
return obj.getBody() + "";
} catch (HttpRequestMethodNotSupportedException ex) {
LOG.error("Cannot authenticate and generate token.");
}
return "";
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment