2 * Copyright (C) 2019 Bell Canada.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
16 package org.onap.ccsdk.apps.blueprintsprocessor.security;
18 import com.google.common.base.Strings;
19 import io.grpc.Metadata;
20 import io.grpc.ServerCall;
21 import io.grpc.ServerCallHandler;
22 import io.grpc.ServerInterceptor;
23 import io.grpc.Status;
24 import java.nio.charset.StandardCharsets;
25 import java.util.Base64;
26 import org.slf4j.Logger;
27 import org.slf4j.LoggerFactory;
28 import org.springframework.beans.factory.annotation.Autowired;
29 import org.springframework.security.authentication.BadCredentialsException;
30 import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
31 import org.springframework.security.core.Authentication;
32 import org.springframework.security.core.AuthenticationException;
33 import org.springframework.security.core.context.SecurityContextHolder;
34 import org.springframework.stereotype.Component;
37 public class BasicAuthServerInterceptor implements ServerInterceptor {
39 private static Logger log = LoggerFactory.getLogger(BasicAuthServerInterceptor.class);
42 private AuthenticationManager authenticationManager;
46 public <ReqT, RespT> ServerCall.Listener<ReqT> interceptCall(
47 ServerCall<ReqT, RespT> call,
49 ServerCallHandler<ReqT, RespT> next) {
50 String authHeader = headers.get(Metadata.Key.of("Authorization", Metadata.ASCII_STRING_MARSHALLER));
52 if (Strings.isNullOrEmpty(authHeader)) {
53 throw Status.UNAUTHENTICATED.withDescription("Missing required authentication").asRuntimeException();
58 String[] tokens = decodeBasicAuth(authHeader);
59 String username = tokens[0];
61 log.info("Basic Authentication Authorization header found for user: {}", username);
63 Authentication authRequest = new UsernamePasswordAuthenticationToken(username, tokens[1]);
64 Authentication authResult = authenticationManager.authenticate(authRequest).block();
66 log.info("Authentication success: {}", authResult);
68 SecurityContextHolder.getContext().setAuthentication(authResult);
70 } catch (AuthenticationException e) {
71 SecurityContextHolder.clearContext();
73 log.info("Authentication request failed: {}", e.getMessage());
75 throw Status.UNAUTHENTICATED.withDescription(e.getMessage()).withCause(e).asRuntimeException();
78 return next.startCall(call, headers);
81 private String[] decodeBasicAuth(String authHeader) {
84 basicAuth = new String(Base64.getDecoder().decode(authHeader.substring(6).getBytes(StandardCharsets.UTF_8)),
85 StandardCharsets.UTF_8);
86 } catch (IllegalArgumentException | IndexOutOfBoundsException e) {
87 throw new BadCredentialsException("Failed to decode basic authentication token");
90 int delim = basicAuth.indexOf(':');
92 throw new BadCredentialsException("Failed to decode basic authentication token");
95 return new String[]{basicAuth.substring(0, delim), basicAuth.substring(delim + 1)};