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
19 import org.onap.ccsdk.cds.controllerblueprints.core.logger
20 import org.springframework.security.authentication.BadCredentialsException
21 import org.springframework.security.authentication.UsernamePasswordAuthenticationToken
22 import org.springframework.security.core.AuthenticationException
23 import org.springframework.security.core.context.SecurityContextHolder
24 import org.springframework.stereotype.Component
25 import java.nio.charset.StandardCharsets
29 class BasicAuthServerInterceptor(private val authenticationManager: AuthenticationManager)
32 private val log = logger(BasicAuthServerInterceptor::class)
34 override fun <ReqT, RespT> interceptCall(
35 call: ServerCall<ReqT, RespT>,
37 next: ServerCallHandler<ReqT, RespT>): ServerCall.Listener<ReqT> {
38 val authHeader = headers.get(Metadata.Key.of("Authorization", Metadata.ASCII_STRING_MARSHALLER))
40 if (authHeader.isNullOrEmpty()) {
41 throw Status.UNAUTHENTICATED.withDescription("Missing required authentication")
46 val tokens = decodeBasicAuth(authHeader)
47 val username = tokens[0]
49 log.info("Basic Authentication Authorization header found for user: {}", username)
51 val authRequest = UsernamePasswordAuthenticationToken(username, tokens[1])
52 val authResult = authenticationManager.authenticate(authRequest).block()
54 log.info("Authentication success: {}", authResult)
56 SecurityContextHolder.getContext().authentication = authResult
58 } catch (e: AuthenticationException) {
59 SecurityContextHolder.clearContext()
61 log.info("Authentication request failed: {}", e.message)
63 throw Status.UNAUTHENTICATED.withDescription(e.message).withCause(e).asRuntimeException()
66 return next.startCall(call, headers)
69 private fun decodeBasicAuth(authHeader: String): Array<String> {
72 basicAuth = String(Base64.getDecoder().decode(authHeader.substring(6).toByteArray(StandardCharsets.UTF_8)),
73 StandardCharsets.UTF_8)
74 } catch (e: IllegalArgumentException) {
75 throw BadCredentialsException("Failed to decode basic authentication token")
76 } catch (e: IndexOutOfBoundsException) {
77 throw BadCredentialsException("Failed to decode basic authentication token")
80 val delim = basicAuth.indexOf(':')
82 throw BadCredentialsException("Failed to decode basic authentication token")
85 return arrayOf(basicAuth.substring(0, delim), basicAuth.substring(delim + 1))