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.cds.blueprintsprocessor.security
18 import io.grpc.Metadata
19 import io.grpc.ServerCall
20 import io.grpc.ServerCallHandler
21 import io.grpc.ServerInterceptor
23 import org.onap.ccsdk.cds.controllerblueprints.core.logger
24 import org.springframework.security.authentication.BadCredentialsException
25 import org.springframework.security.authentication.UsernamePasswordAuthenticationToken
26 import org.springframework.security.core.AuthenticationException
27 import org.springframework.security.core.context.SecurityContextHolder
28 import org.springframework.stereotype.Component
29 import java.nio.charset.StandardCharsets
30 import java.util.Base64
33 class BasicAuthServerInterceptor(private val authenticationManager: AuthenticationManager) :
36 private val log = logger(BasicAuthServerInterceptor::class)
38 override fun <ReqT, RespT> interceptCall(
39 call: ServerCall<ReqT, RespT>,
41 next: ServerCallHandler<ReqT, RespT>
42 ): ServerCall.Listener<ReqT> {
43 val authHeader = headers.get(Metadata.Key.of("Authorization", Metadata.ASCII_STRING_MARSHALLER))
45 if (authHeader.isNullOrEmpty()) {
46 throw Status.UNAUTHENTICATED.withDescription("Missing required authentication")
51 val tokens = decodeBasicAuth(authHeader)
52 val username = tokens[0]
54 log.info("Basic Authentication Authorization header found for user: {}", username)
56 val authRequest = UsernamePasswordAuthenticationToken(username, tokens[1])
57 val authResult = authenticationManager.authenticate(authRequest).block()
59 log.info("Authentication success: {}", authResult)
61 SecurityContextHolder.getContext().authentication = authResult
62 } catch (e: AuthenticationException) {
63 SecurityContextHolder.clearContext()
65 log.info("Authentication request failed: {}", e.message)
67 throw Status.UNAUTHENTICATED.withDescription(e.message).withCause(e).asRuntimeException()
70 return next.startCall(call, headers)
73 private fun decodeBasicAuth(authHeader: String): Array<String> {
77 Base64.getDecoder().decode(authHeader.substring(6).toByteArray(StandardCharsets.UTF_8)),
78 StandardCharsets.UTF_8
80 } catch (e: IllegalArgumentException) {
81 throw BadCredentialsException("Failed to decode basic authentication token")
82 } catch (e: IndexOutOfBoundsException) {
83 throw BadCredentialsException("Failed to decode basic authentication token")
86 val delim = basicAuth.indexOf(':')
88 throw BadCredentialsException("Failed to decode basic authentication token")
91 return arrayOf(basicAuth.substring(0, delim), basicAuth.substring(delim + 1))