| 1 | package Zing::Encoder::Jwt; | ||
| 2 | |||
| 3 | use 5.014; | ||
| 4 | |||
| 5 | use strict; | ||
| 6 | use warnings; | ||
| 7 | |||
| 8 | use registry 'Zing::Types'; | ||
| 9 | use routines; | ||
| 10 | |||
| 11 | use Data::Object::Class; | ||
| 12 | use Data::Object::ClassHas; | ||
| 13 | |||
| 14 | extends 'Zing::Encoder'; | ||
| 15 | |||
| 16 | use Crypt::JWT (); | ||
| 17 | |||
| 18 | our $VERSION = '0.01'; # VERSION | ||
| 19 | |||
| 20 | # ATTRIBUTES | ||
| 21 | |||
| 22 | has algo => ( | ||
| 23 | is => 'ro', | ||
| 24 | isa => 'Str', | ||
| 25 | new => 1, | ||
| 26 | ); | ||
| 27 | |||
| 28 | fun new_algo($self) { | ||
| 29 | $ENV{ZING_JWT_ALGO} || 'HS256' | ||
| 30 | } | ||
| 31 | |||
| 32 | has secret => ( | ||
| 33 | is => 'ro', | ||
| 34 | isa => 'Str', | ||
| 35 | new => 1, | ||
| 36 | ); | ||
| 37 | |||
| 38 | fun new_secret($self) { | ||
| 39 | $ENV{ZING_JWT_SECRET} | ||
| 40 | } | ||
| 41 | |||
| 42 | # METHODS | ||
| 43 | |||
| 44 | method decode(Str $data) { | ||
| 45 | return Crypt::JWT::decode_jwt( | ||
| 46 | key => $self->secret, | ||
| 47 | token => $data, | ||
| 48 | ); | ||
| 49 | } | ||
| 50 | |||
| 51 | method encode(HashRef $data) { | ||
| 52 | return Crypt::JWT::encode_jwt( | ||
| 53 | alg => $self->algo, | ||
| 54 | key => $self->secret, | ||
| 55 | payload => $data, | ||
| 56 | ); | ||
| 57 | } | ||
| 58 | |||
| 59 | 1; | ||
| 60 | |||
| 61 | =encoding utf8 | ||
| 62 | |||
| 63 | =head1 NAME | ||
| 64 | |||
| 65 | Zing::Encoder::Jwt - JWT Serialization Abstraction | ||
| 66 | |||
| 67 | =cut | ||
| 68 | |||
| 69 | =head1 ABSTRACT | ||
| 70 | |||
| 71 | JWT Data Serialization Abstraction | ||
| 72 | |||
| 73 | =cut | ||
| 74 | |||
| 75 | =head1 SYNOPSIS | ||
| 76 | |||
| 77 | use Zing::Encoder::Jwt; | ||
| 78 | |||
| 79 | my $encoder = Zing::Encoder::Jwt->new( | ||
| 80 | secret => '...', | ||
| 81 | ); | ||
| 82 | |||
| 83 | # $encoder->encode({ status => 'okay' }); | ||
| 84 | |||
| 85 | =cut | ||
| 86 | |||
| 87 | =head1 DESCRIPTION | ||
| 88 | |||
| 89 | This package provides a L<Crypt::JWT> data serialization abstraction for use | ||
| 90 | with L<Zing::Store> stores. The JWT encoding algorithm can be set using the | ||
| 91 | C<ZING_JWT_ALGO> environment variable or the I<algo> attribute, and defaults to | ||
| 92 | I<HS256>. The JWT secret can be set using the C<ZING_JWT_SECRET> environment | ||
| 93 | variable or the I<secret> attribute. | ||
| 94 | |||
| 95 | =cut | ||
| 96 | |||
| 97 | =head1 LIBRARIES | ||
| 98 | |||
| 99 | This package uses type constraints from: | ||
| 100 | |||
| 101 | L<Zing::Types> | ||
| 102 | |||
| 103 | =cut | ||
| 104 | |||
| 105 | =head1 METHODS | ||
| 106 | |||
| 107 | This package implements the following methods: | ||
| 108 | |||
| 109 | =cut | ||
| 110 | |||
| 111 | =head2 decode | ||
| 112 | |||
| 113 | decode(Str $data) : HashRef | ||
| 114 | |||
| 115 | The decode method decodes the data provided. | ||
| 116 | |||
| 117 | =over 4 | ||
| 118 | |||
| 119 | =item decode example #1 | ||
| 120 | |||
| 121 | # given: synopsis | ||
| 122 | |||
| 123 | $encoder->decode('eyJhbGciOiJIUzI1NiJ9.eyJzdGF0dXMiOiJva2F5In0.tXdQmMPi25VOJZaOySFS-hM2ofIxbyFBVTA7I-GI_lU'); | ||
| 124 | |||
| 125 | =back | ||
| 126 | |||
| 127 | =cut | ||
| 128 | |||
| 129 | =head2 encode | ||
| 130 | |||
| 131 | encode(HashRef $data) : Str | ||
| 132 | |||
| 133 | The encode method encodes the data provided. | ||
| 134 | |||
| 135 | =over 4 | ||
| 136 | |||
| 137 | =item encode example #1 | ||
| 138 | |||
| 139 | # given: synopsis | ||
| 140 | |||
| 141 | $encoder->encode({ status => 'okay' }); | ||
| 142 | |||
| 143 | =back | ||
| 144 | |||
| 145 | =cut | ||
| 146 | |||
| 147 | =head1 AUTHOR | ||
| 148 | |||
| 149 | Al Newkirk, C<awncorp@cpan.org> | ||
| 150 | |||
| 151 | =head1 LICENSE | ||
| 152 | |||
| 153 | Copyright (C) 2011-2019, Al Newkirk, et al. | ||
| 154 | |||
| 155 | This is free software; you can redistribute it and/or modify it under the terms | ||
| 156 | of the The Apache License, Version 2.0, as elucidated in the L<"license | ||
| 157 | file"|https://github.com/iamalnewkirk/zing-encoder-jwt/blob/master/LICENSE>. | ||
| 158 | |||
| 159 | =head1 PROJECT | ||
| 160 | |||
| 161 | L<Wiki|https://github.com/iamalnewkirk/zing-encoder-jwt/wiki> | ||
| 162 | |||
| 163 | L<Project|https://github.com/iamalnewkirk/zing-encoder-jwt> | ||
| 164 | |||
| 165 | L<Initiatives|https://github.com/iamalnewkirk/zing-encoder-jwt/projects> | ||
| 166 | |||
| 167 | L<Milestones|https://github.com/iamalnewkirk/zing-encoder-jwt/milestones> | ||
| 168 | |||
| 169 | L<Contributing|https://github.com/iamalnewkirk/zing-encoder-jwt/blob/master/CONTRIBUTE.md> | ||
| 170 | |||
| 171 | L<Issues|https://github.com/iamalnewkirk/zing-encoder-jwt/issues> | ||
| 172 | |||
| 173 | =cut |