mirror of
https://github.com/fhem/fhem-mirror.git
synced 2025-04-08 01:14:19 +00:00
./contrib/98_GoogleAuth.pm: new features
git-svn-id: https://svn.fhem.de/fhem/trunk@13081 2b470e98-0d58-463d-a4d8-8e2adae1ed80
This commit is contained in:
parent
9e337e1e12
commit
dc994c59fd
@ -4,12 +4,21 @@
|
||||
#
|
||||
# Development history
|
||||
#
|
||||
# 2017-01-15 - first check in to ./contrib
|
||||
# 2017-01-15 - added: direct QR display after set
|
||||
# added: attribute ga_qrSize
|
||||
# added: FW_detailFn
|
||||
# added: attribute ga_labelName
|
||||
# added: reading lastCheck
|
||||
# 2017-01-15 - first commit to ./contrib
|
||||
# 2017-01-15 - added: direct QR display after set
|
||||
# added: attribute ga_qrSize
|
||||
# added: FW_detailFn
|
||||
# added: attribute ga_labelName
|
||||
# added: reading lastCheck
|
||||
#
|
||||
# removed: reading qr_url
|
||||
# added: show link to qrcode and key for manual use
|
||||
# in device details
|
||||
# added: set command "revoke" to prevent overwrite
|
||||
# of existing key
|
||||
# added: attribute ga_showKey
|
||||
# attribute ga_showLink
|
||||
# added: function gAuth(<device>,<token>) for easy use
|
||||
#
|
||||
=cut
|
||||
|
||||
@ -34,8 +43,10 @@ sub GoogleAuth_Initialize($) {
|
||||
$hash->{GetFn} = "GoogleAuth_Get";
|
||||
$hash->{FW_detailFn} = "GoogleAuth_Detail";
|
||||
# $hash->{AttrFn} = "GoogleAuth_Attr";
|
||||
$hash->{AttrList} = "ga_qrSize ".
|
||||
"ga_labelName ".
|
||||
$hash->{AttrList} = "ga_labelName ".
|
||||
"ga_qrSize:100x100,200x200,300x300,400x400 ".
|
||||
"ga_showKey:0,1 ".
|
||||
"ga_showLink:0,1 ".
|
||||
"$readingFnAttributes";
|
||||
}
|
||||
|
||||
@ -50,35 +61,31 @@ sub GoogleAuth_Define($$) {
|
||||
return undef;
|
||||
}
|
||||
|
||||
sub GoogleAuth_Delete() {
|
||||
my ($hash, $arg) = @_;
|
||||
my $name = $hash->{NAME};
|
||||
sub GoogleAuth_Delete($$) {
|
||||
my ($hash,$name) = @_;
|
||||
setKeyValue("googleAuth$name",undef);
|
||||
}
|
||||
|
||||
sub GoogleAuth_Set($$@) {
|
||||
my ($hash, $name, $cmd, @args) = @_;
|
||||
my $usage = "Unknown argument, choose one of new:noArg";
|
||||
my $usage = "Unknown argument, choose one of new:noArg revoke:noArg";
|
||||
|
||||
if($cmd eq "new") {
|
||||
#SOURCE: https://blog.darkpan.com/article/6/Perl-and-Google-Authenticator.html
|
||||
return "Please revoke existing key first!" if defined(getKeyValue("googleAuth$name"));
|
||||
my $secret_bytes = urandom(50);
|
||||
my $secret_base32 = encode_base32( $secret_bytes );
|
||||
Log3($hash,5,"googleAuth $name: secret_bytes=$secret_bytes");
|
||||
Log3($hash,5,"googleAuth $name: set secret_base32=$secret_base32");
|
||||
|
||||
setKeyValue("googleAuth$name",$secret_base32); # write to fhem keystore
|
||||
|
||||
my $label = AttrVal($name,'ga_labelName',"FHEM Authentication $name");
|
||||
my $qrsize = AttrVal($name,'ga_qrSize','200x200');
|
||||
my $url = "otpauth://totp/$label?secret=$secret_base32";
|
||||
my $qr_url = "https://chart.googleapis.com/chart?cht=qr&chs=$qrsize"."&chl=".uri_escape($url);
|
||||
|
||||
readingsSingleUpdate($hash,'qr_url',$qr_url,0);
|
||||
readingsSingleUpdate($hash,'state','active',1);
|
||||
return undef;
|
||||
} elsif ($cmd eq "revoke") {
|
||||
setKeyValue("googleAuth$name",undef);
|
||||
readingsSingleUpdate($hash,'state','defined',1);
|
||||
} else {
|
||||
return $usage
|
||||
}
|
||||
return $usage;
|
||||
return undef;
|
||||
}
|
||||
|
||||
sub GoogleAuth_Get($$@) {
|
||||
@ -110,16 +117,37 @@ sub GoogleAuth_Get($$@) {
|
||||
}
|
||||
|
||||
sub GoogleAuth_Detail($@) {
|
||||
my ($FW_wname, $d, $room, $pageHash) = @_;
|
||||
my $qr_url = ReadingsVal($d,'qr_url',undef);
|
||||
my ($FW_wname, $name, $room, $pageHash) = @_;
|
||||
my $qr_url = _ga_make_url($name);
|
||||
my $secret_base32 = getKeyValue("googleAuth$name"); # read from fhem keystore
|
||||
|
||||
# my $qr_url = ReadingsVal($d,'qr_url',undef);
|
||||
return unless defined($qr_url);
|
||||
my $ret = "<a href=\"$qr_url\"><img src=\"$qr_url\"><\/a><br>";
|
||||
my $ret = "<table>";
|
||||
$ret .= "<tr><td rowspan=2><a href=\"$qr_url\"><img src=\"$qr_url\"><\/a></td>";
|
||||
$ret .= "<td><br> <a href=\"$qr_url\">Link to QR code<\/a><\/td>"
|
||||
if AttrVal($name,'ga_showLink',0);
|
||||
$ret .= "</tr>";
|
||||
$ret .= "<tr><td> Key (for manual use):<br> $secret_base32</td><tr>"
|
||||
if AttrVal($name,'ga_showKey',0);
|
||||
$ret .= "</table>";
|
||||
return $ret;
|
||||
}
|
||||
|
||||
|
||||
|
||||
# helper functions
|
||||
sub _ga_make_url($) {
|
||||
my ($name) = @_;
|
||||
my $label = AttrVal($name,'ga_labelName',"FHEM Authentication $name");
|
||||
my $qrsize = AttrVal($name,'ga_qrSize','200x200');
|
||||
my $secret_base32 = getKeyValue("googleAuth$name");
|
||||
return undef unless defined($secret_base32);
|
||||
my $url = "otpauth://totp/$label?secret=$secret_base32";
|
||||
my $qr_url = "https://chart.googleapis.com/chart?cht=qr&chs=$qrsize"."&chl=";
|
||||
$qr_url .= uri_escape($url);
|
||||
return $qr_url;
|
||||
}
|
||||
|
||||
sub _ga_make_token_6($) {
|
||||
my $token = shift;
|
||||
@ -129,6 +157,10 @@ sub _ga_make_token_6($) {
|
||||
return $token;
|
||||
}
|
||||
|
||||
sub gAuth($$) {
|
||||
my($name,$token) = @_;
|
||||
return CommandGet(undef,"$name check $token");
|
||||
}
|
||||
|
||||
1;
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user