7
5

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

gRPCでユーザ/パスワード認証をする

Last updated at Posted at 2017-10-19

環境

| Client | Server
-----|-----|-----
言語 | Ruby | Go

参考にしたコード

https://github.com/grpc/grpc-go/issues/106#issuecomment-246978683
(clientとserverを両方goで実装した例)

ソースコード

gRPCリクエストのメタデータでusernamepasswordを渡し、認証をします。

Client

client.rb
stub = HogeServer::Stub.new("localhost:99999", :this_channel_is_insecure)
request = HogeRequest.new(hoge: 'fuga')
metadata = { username: 'admin', password: 'admin123' }
stub.hoge(request, metadata: metadata)

Server

server.go
import (
	grpc "google.golang.org/grpc"
	metadata "google.golang.org/grpc/metadata"
)

grpcServer := grpc.NewServer(
	grpc.StreamInterceptor(streamInterceptor),
	grpc.UnaryInterceptor(unaryInterceptor),
)

func streamInterceptor(srv interface{}, stream grpc.ServerStream, info *grpc.StreamServerInfo, handler grpc.StreamHandler) error {
	if err := authorize(stream.Context()); err != nil {
		return err
	}
	return handler(srv, stream)
}

func unaryInterceptor(ctx context.Context, req interface{}, info *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (interface{}, error) {
	if err := authorize(ctx); err != nil {
		return nil, err
	}
	return handler(ctx, req)
}

func authorize(ctx context.Context) error {
	if md, ok := metadata.FromIncomingContext(ctx); ok {
		if len(md["username"]) > 0 && md["username"][0] == "admin" &&
			len(md["password"]) > 0 && md["password"][0] == "admin123" {
			return nil
		}
		return errors.New("AccessDeniedError")
	}
	return errors.New("EmptyMetadataError")
}
7
5
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
7
5

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?