1/*
2 *
3 * Copyright 2020 gRPC authors.
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 *     http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 *
17 */
18
19// Package insecure provides an implementation of the
20// credentials.TransportCredentials interface which disables transport security.
21//
22// Experimental
23//
24// Notice: This package is EXPERIMENTAL and may be changed or removed in a
25// later release.
26package insecure
27
28import (
29	"context"
30	"net"
31
32	"google.golang.org/grpc/credentials"
33)
34
35// NewCredentials returns a credentials which disables transport security.
36func NewCredentials() credentials.TransportCredentials {
37	return insecureTC{}
38}
39
40// insecureTC implements the insecure transport credentials. The handshake
41// methods simply return the passed in net.Conn and set the security level to
42// NoSecurity.
43type insecureTC struct{}
44
45func (insecureTC) ClientHandshake(ctx context.Context, _ string, conn net.Conn) (net.Conn, credentials.AuthInfo, error) {
46	return conn, info{credentials.CommonAuthInfo{SecurityLevel: credentials.NoSecurity}}, nil
47}
48
49func (insecureTC) ServerHandshake(conn net.Conn) (net.Conn, credentials.AuthInfo, error) {
50	return conn, info{credentials.CommonAuthInfo{SecurityLevel: credentials.NoSecurity}}, nil
51}
52
53func (insecureTC) Info() credentials.ProtocolInfo {
54	return credentials.ProtocolInfo{SecurityProtocol: "insecure"}
55}
56
57func (insecureTC) Clone() credentials.TransportCredentials {
58	return insecureTC{}
59}
60
61func (insecureTC) OverrideServerName(string) error {
62	return nil
63}
64
65// info contains the auth information for an insecure connection.
66// It implements the AuthInfo interface.
67type info struct {
68	credentials.CommonAuthInfo
69}
70
71// AuthType returns the type of info as a string.
72func (info) AuthType() string {
73	return "insecure"
74}
75