001/* 002 * Licensed under the Apache License, Version 2.0 (the "License"); 003 * you may not use this file except in compliance with the License. 004 * You may obtain a copy of the License at 005 * 006 * http://www.apache.org/licenses/LICENSE-2.0 007 * 008 * Unless required by applicable law or agreed to in writing, software 009 * distributed under the License is distributed on an "AS IS" BASIS, 010 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 011 * See the License for the specific language governing permissions and 012 * limitations under the License. 013 */ 014package org.gbif.ws.remoteauth.basic; 015 016import org.gbif.ws.remoteauth.AbstractRemoteAuthenticationProvider; 017import org.gbif.ws.remoteauth.RemoteAuthClient; 018import org.gbif.ws.security.GbifAuthenticationToken; 019import org.gbif.ws.security.GbifUserPrincipal; 020 021import org.springframework.http.HttpHeaders; 022import org.springframework.http.ResponseEntity; 023import org.springframework.security.authentication.UsernamePasswordAuthenticationToken; 024import org.springframework.security.core.Authentication; 025 026import lombok.extern.slf4j.Slf4j; 027 028/** 029 * Basic authentication against the registry. 030 */ 031@Slf4j 032public class BasicRemoteAuthenticationProvider 033 extends AbstractRemoteAuthenticationProvider<UsernamePasswordAuthenticationToken> { 034 035 private static final String AUTH_PATH = "/user/auth/basic"; 036 037 public BasicRemoteAuthenticationProvider(RemoteAuthClient remoteAuthClient) { 038 super(UsernamePasswordAuthenticationToken.class, AUTH_PATH, remoteAuthClient); 039 } 040 041 @Override 042 public HttpHeaders createHttpHeaders(Authentication authentication) { 043 HttpHeaders headers = new HttpHeaders(); 044 headers.setBasicAuth(authentication.getName(), authentication.getCredentials().toString()); 045 return headers; 046 } 047 048 @Override 049 protected Authentication createSuccessAuthentication( 050 ResponseEntity<String> response, Authentication authentication) { 051 GbifUserPrincipal gbifUserPrincipal = 052 new GbifUserPrincipal(readUserFromResponse(response).toGbifUser()); 053 return new GbifAuthenticationToken(gbifUserPrincipal, gbifUserPrincipal.getAuthorities()); 054 } 055}