<html><head><meta name="color-scheme" content="light dark"></head><body><pre style="word-wrap: break-word; white-space: pre-wrap;">package yifangcloud;
use Crypt::JWT qw(encode_jwt);
use Encode;
use IO::File;
use JSON;
use LWP::UserAgent;
use MIME::Base64;
our @EXPORT = qw/ get_user randstr token /;

my $conf_file = "conf.json";
my $rsa_file = "rsa_private_key.pem";
my $user_file = "ypusers.json";
my $token_url = "https://oauth.fangcloud.com/oauth/token";
my $admin_url = "https://open.fangcloud.com/api/v2/admin";

=get_department
get_department(HASH)
=cut

sub get_department {
    if ( @_ &amp;&amp; ref $_[0] eq 'HASH' ) {
        my $json = $_[0];
        my $dp_id = $json-&gt;{did} || 0;
        my $reply;
        my $ua = LWP::UserAgent-&gt;new;
        $ua-&gt;timeout(10);
        $ua-&gt;env_proxy;
        my $res = $ua-&gt;default_header(Authorization =&gt; "$json-&gt;{token_type} $json-&gt;{access_token}",);
        $res = $ua-&gt;get("$admin_url/department/$dp_id/children");
        if ( $res-&gt;is_success ) {
            $reply-&gt;{status} = 0;
        }
        else {
            $reply-&gt;{status} = $res-&gt;status_line;
        }
        $reply-&gt;{content} = decode_json($res-&gt;decoded_content);
        return $reply;
    }
}

=get_group
get_group(HASH)
=cut

sub get_group {
    if ( @_ &amp;&amp; ref $_[0] eq 'HASH' ) {
        my $json = $_[0];
        my $reply;
        my $ua = LWP::UserAgent-&gt;new;
        $ua-&gt;timeout(10);
        $ua-&gt;env_proxy;
        my $res = $ua-&gt;default_header(Authorization =&gt; "$json-&gt;{token_type} $json-&gt;{access_token}",);
        $res = $ua-&gt;get("$admin_url/group/list");
        if ( $res-&gt;is_success ) {
            $reply-&gt;{status} = 0;
        }
        else {
            $reply-&gt;{status} = $res-&gt;status_line;
        }
        $reply-&gt;{content} = decode_json($res-&gt;decoded_content);
        return $reply;
    }
}

=get_group_users
get_group_users(HASH)
=cut

sub get_group_users {
    if ( @_ &amp;&amp; ref $_[0] eq 'HASH' ) {
        my $json = $_[0];
        my $dp_id = $json-&gt;{gid} || 0;
        my $reply;
        my $ua = LWP::UserAgent-&gt;new;
        $ua-&gt;timeout(10);
        $ua-&gt;env_proxy;
        my $res = $ua-&gt;default_header(Authorization =&gt; "$json-&gt;{token_type} $json-&gt;{access_token}",);
        $res = $ua-&gt;get("$admin_url/group/$dp_id/users");
        if ( $res-&gt;is_success ) {
            $reply-&gt;{status} = 0;
        }
        else {
            $reply-&gt;{status} = $res-&gt;status_line;
        }
        $reply-&gt;{content} = decode_json($res-&gt;decoded_content);
        return $reply;
    }
}

=get_user
get_user(HASH)
=cut

sub get_user {
    if ( @_ &amp;&amp; ref $_[0] eq 'HASH' ) {
        my $json = $_[0];
        my $count = 1;
        my $page_id = 0;
        my $dp_id = $json-&gt;{cid} || 0;
        my $reply;
        my $users = [];
        my $fh = new IO::File $user_file, "r";
        if ( defined $fh ) {
            $users = from_json(&lt;$fh&gt;);
            undef $fh;
        }
        my $ua = LWP::UserAgent-&gt;new;
        $ua-&gt;timeout(10);
        $ua-&gt;env_proxy;
        my $res = $ua-&gt;default_header(Authorization =&gt; "$json-&gt;{token_type} $json-&gt;{access_token}",);
        while ( $page_id &lt; $count ) {
            $res = $ua-&gt;get("$admin_url/department/$dp_id/users?page_id=$page_id");
            if ( $res-&gt;is_success ) {
                $reply-&gt;{status} = 0;
                my $temp = decode_json($res-&gt;decoded_content);
                if ( $temp-&gt;{total_count} == $#{$users} + 1 ) {
                    $reply-&gt;{content}-&gt;{users} = $users;
                    $reply-&gt;{content}-&gt;{total_count} = $temp-&gt;{total_count};
                    return $reply;
                }
                if ( $page_id ) {
                    $reply-&gt;{content}-&gt;{users} = [@{$reply-&gt;{content}-&gt;{users}},@{$temp-&gt;{users}}];
                }
                else {
                    $reply-&gt;{content} = $temp;
                    $count = $reply-&gt;{content}-&gt;{page_count};
                }
            }
            else {
                $reply-&gt;{status} = $res-&gt;status_line;
                $reply-&gt;{content} = decode_json($res-&gt;decoded_content);
            }
            $page_id++;
        }
        unless ( $dp_id ) {
            $fh = new IO::File $user_file, "w";
            if ( defined $fh ) {
                $users = encode_json($reply-&gt;{content}-&gt;{users});
                print $fh $users;
                undef $fh;
            }
        }
        return $reply;
    }
}

=randstr
randstr(INT)
=cut

sub randstr {
    if ( @_ ) {
        my $i = $_[0];
        if ( $i =~ /^\d+$/ ) {
            my @charset = (0..9,'a'..'z','A'..'Z');
            my $str = join '', map{$charset[int rand @charset]} 0..$i;
            return $str;
        }
    }
}

=search
search(HASH)
=cut

sub search {
    if ( @_ &amp;&amp; ref $_[0] eq 'HASH' ) {
        my $json = $_[0];
        $ua = LWP::UserAgent-&gt;new;
        $ua-&gt;timeout(10);
        $ua-&gt;env_proxy;
        $res = $ua-&gt;default_header(Authorization =&gt; "$json-&gt;{token_type} $json-&gt;{access_token}",);
        $res = $ua-&gt;get("$admin_url/user/$json-&gt;{uid}/info");
        my $reply;
        if ( $res-&gt;is_success ) {
            $reply-&gt;{status} = 0;
        }
        else {
            $reply-&gt;{status} = $res-&gt;status_line;
        }
        $reply-&gt;{content} = decode_json($res-&gt;decoded_content);
        return $reply;
    }
}

=token
token()
=cut

sub token() {
    my $fh = new IO::File $conf_file, "r";
    if ( defined $fh ) {
        my $json = join('',&lt;$fh&gt;);
        $json =~ s/\s//g;
        my $conf = from_json($json);
        undef $fh;
        $fh = new IO::File $rsa_file, "r";
        if ( defined $fh ) {
            my $pem_key_string = join('',&lt;$fh&gt;);
            undef $fh;
            $conf-&gt;{claims}-&gt;{exp} = time() + 30;
            $conf-&gt;{claims}-&gt;{jti} = randstr(10) . randstr(10);
            my $encrypt = encode_base64($conf-&gt;{client}-&gt;{id} . ":" . $conf-&gt;{client}-&gt;{secret});
            my $token = encode_jwt(payload =&gt; encode_json($conf-&gt;{claims}),
                                   alg =&gt; $conf-&gt;{header}-&gt;{alg},
                                   key =&gt; \$pem_key_string,
                                   extra_headers =&gt; {kid =&gt; $conf-&gt;{header}-&gt;{kid}});
            my $ua = LWP::UserAgent-&gt;new;
            $ua-&gt;timeout(10);
            $ua-&gt;env_proxy;
            my $res = $ua-&gt;default_header(Authorization =&gt; "Basic $encrypt");
            $res = $ua-&gt;post("$token_url?grant_type=jwt&amp;assertion=$token");
            my $reply;
            if ( $res-&gt;is_success ) {
                $reply-&gt;{status} = 0;
            }
            else {
                $reply-&gt;{status} = $res-&gt;status_line;
            }
            $reply-&gt;{content} = decode_json($res-&gt;decoded_content);
            return $reply;
        }
    }
}

=user_create
user_create(HASH)
=cut

sub user_create {
    if ( @_ &amp;&amp; ref $_[0] eq 'HASH' ) {
        my $json = $_[0];
        if ( $json-&gt;{users}-&gt;[0]-&gt;{id} =~ /^\d+$/ ) {
            my $data;
            $data-&gt;{users} = $json-&gt;{users};
            $ua = LWP::UserAgent-&gt;new;
            $ua-&gt;timeout(10);
            $ua-&gt;env_proxy;
            $res = $ua-&gt;default_header(Authorization =&gt; "$json-&gt;{token_type} $json-&gt;{access_token}",);
            $res = $ua-&gt;post("$admin_url/platform/20/sync_users",Content =&gt; encode_json($data),Content_type =&gt;'application/json');
            my $reply;
            if ( $res-&gt;is_success ) {
                $reply-&gt;{status} = 0;
            }
            else {
                $reply-&gt;{status} = $res-&gt;status_line;
            }
            $reply-&gt;{content} = decode_json($res-&gt;decoded_content);
            return $reply;
        }
    }
}

=encoding utf8

= cut

1;
__END__
</pre></body></html>